wraith-lang/wraith.y

299 lines
9.1 KiB
Plaintext

%code requires {
#include "stack.h"
}
%{
#include <stdio.h>
#include "ast.h"
#include "stack.h"
void yyerror(FILE *fp, Stack *stack, char *s)
{
fprintf (stderr, "%s\n", s);
}
extern char *yytext;
extern int yylex (void);
extern FILE *yyin;
extern Node *rootNode;
%}
%define api.value.type {struct Node*}
%token VOID
%token INT
%token UINT
%token FLOAT
%token DOUBLE
%token STRING
%token BOOL
%token STRUCT
%token RETURN
%token NUMBER
%token ID
%token STRING_LITERAL
%token PLUS
%token MINUS
%token STAR
%token SLASH
%token PERCENT
%token EQUAL
%token LESS_THAN
%token GREATER_THAN
%token QUOTE
%token BANG
%token BAR
%token AMPERSAND
%token POINT
%token COMMA
%token SEMICOLON
%token COLON
%token QUESTION
%token LEFT_PAREN
%token RIGHT_PAREN
%token LEFT_BRACE
%token RIGHT_BRACE
%token LEFT_BRACKET
%token RIGHT_BRACKET
%token COMMENT
%token NEWLINE
%parse-param { FILE* fp } { Stack *stack }
%left PLUS MINUS
%left BANG
%left LEFT_PAREN RIGHT_PAREN
%%
Program : TopLevelDeclarations
{
Node **declarations;
Node *declarationSequence;
uint32_t declarationCount;
declarations = GetNodes(stack, &declarationCount);
declarationSequence = MakeDeclarationSequenceNode(declarations, declarationCount);
PopStackFrame(stack);
rootNode = declarationSequence;
}
Type : VOID
{
$$ = MakeTypeNode(Void);
}
| INT
{
$$ = MakeTypeNode(Int);
}
| UINT
{
$$ = MakeTypeNode(UInt);
}
| FLOAT
{
$$ = MakeTypeNode(Float);
}
| DOUBLE
{
$$ = MakeTypeNode(Double);
}
| STRING
{
$$ = MakeTypeNode(String);
}
| BOOL
{
$$ = MakeTypeNode(Bool);
}
;
Identifier : ID
{
$$ = MakeIdentifierNode(yytext);
}
PrimaryExpression : Identifier
| NUMBER
{
$$ = MakeNumberNode(yytext);
}
| STRING
{
$$ = MakeStringNode(yytext);
}
| LEFT_PAREN Expression RIGHT_PAREN
{
$$ = $2;
}
| FunctionCallExpression
;
UnaryExpression : BANG Expression
{
$$ = MakeUnaryNode(Negate, $2);
}
BinaryExpression : Expression PLUS Expression
{
$$ = MakeBinaryNode(Add, $1, $3);
}
| Expression MINUS Expression
{
$$ = MakeBinaryNode(Subtract, $1, $3);
}
| Expression STAR Expression
{
$$ = MakeBinaryNode(Multiply, $1, $3);
}
Expression : PrimaryExpression
| UnaryExpression
| BinaryExpression
;
VariableDeclaration : Type Identifier
{
$$ = MakeDeclarationNode($1, $2);
}
AssignmentStatement : VariableDeclaration EQUAL Expression
{
$$ = MakeAssignmentNode($1, $3);
}
| Identifier EQUAL Expression
{
$$ = MakeAssignmentNode($1, $3);
}
ReturnStatement : RETURN Expression
{
$$ = MakeReturnStatementNode($2);
}
| RETURN
{
$$ = MakeReturnVoidStatementNode();
}
FunctionCallExpression : Identifier LEFT_PAREN Arguments RIGHT_PAREN
{
Node **arguments;
uint32_t argumentCount;
arguments = GetNodes(stack, &argumentCount);
$$ = MakeFunctionCallExpressionNode($1, MakeFunctionArgumentSequenceNode(arguments, argumentCount));
PopStackFrame(stack);
}
PartialStatement : AssignmentStatement
| VariableDeclaration
| FunctionCallExpression
| ReturnStatement
;
Statement : PartialStatement SEMICOLON;
Statements : Statement Statements
{
AddNode(stack, $1);
}
|
{
PushStackFrame(stack);
}
Arguments : PrimaryExpression COMMA Arguments
{
AddNode(stack, $1);
}
| PrimaryExpression
{
PushStackFrame(stack);
AddNode(stack, $1);
}
|
;
SignatureArguments : VariableDeclaration COMMA SignatureArguments
{
AddNode(stack, $1);
}
| VariableDeclaration
{
PushStackFrame(stack);
AddNode(stack, $1);
}
|
;
Body : LEFT_BRACE Statements RIGHT_BRACE
{
Node **statements;
Node *statementSequence;
uint32_t statementCount;
statements = GetNodes(stack, &statementCount);
statementSequence = MakeStatementSequenceNode(statements, statementCount);
$$ = MakeStatementSequenceNode(statements, statementCount);
PopStackFrame(stack);
}
FunctionSignature : Type Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN
{
Node **declarations;
uint32_t declarationCount;
declarations = GetNodes(stack, &declarationCount);
$$ = MakeFunctionSignatureNode($2, $1, MakeFunctionSignatureArgumentsNode(declarations, declarationCount));
PopStackFrame(stack);
}
FunctionDeclaration : FunctionSignature Body
{
$$ = MakeFunctionDeclarationNode($1, $2);
}
StructDeclaration : STRUCT Identifier LEFT_BRACE Declarations RIGHT_BRACE
{
Node **declarations;
Node *declarationSequence;
uint32_t declarationCount;
declarations = GetNodes(stack, &declarationCount);
declarationSequence = MakeDeclarationSequenceNode(declarations, declarationCount);
$$ = MakeStructDeclarationNode($2, declarationSequence);
PopStackFrame(stack);
}
Declaration : FunctionDeclaration
| VariableDeclaration SEMICOLON
;
Declarations : Declaration Declarations
{
AddNode(stack, $1);
}
|
{
PushStackFrame(stack);
}
TopLevelDeclaration : StructDeclaration;
TopLevelDeclarations : TopLevelDeclaration TopLevelDeclarations
{
AddNode(stack, $1);
}
|
{
PushStackFrame(stack);
}
%%