wraith-lang/wraith.y

299 lines
9.1 KiB
Plaintext
Raw Normal View History

2021-04-18 22:29:54 +00:00
%code requires {
#include "stack.h"
}
2021-04-16 21:40:28 +00:00
%{
#include <stdio.h>
#include "ast.h"
2021-04-18 20:14:50 +00:00
#include "stack.h"
2021-04-18 22:29:54 +00:00
void yyerror(FILE *fp, Stack *stack, char *s)
2021-04-16 21:40:28 +00:00
{
fprintf (stderr, "%s\n", s);
}
2021-04-18 20:14:50 +00:00
extern char *yytext;
extern int yylex (void);
extern FILE *yyin;
2021-04-18 22:45:06 +00:00
extern Node *rootNode;
2021-04-16 21:40:28 +00:00
%}
%define api.value.type {struct Node*}
2021-04-21 02:00:18 +00:00
%token VOID
2021-04-16 21:40:28 +00:00
%token INT
%token UINT
%token FLOAT
%token DOUBLE
2021-04-16 07:35:35 +00:00
%token STRING
2021-04-16 21:40:28 +00:00
%token BOOL
2021-04-18 20:14:50 +00:00
%token STRUCT
2021-04-18 20:30:50 +00:00
%token RETURN
2021-04-21 02:00:18 +00:00
%token NUMBER
2021-04-16 21:40:28 +00:00
%token ID
%token STRING_LITERAL
2021-04-16 07:35:35 +00:00
%token PLUS
%token MINUS
2021-04-20 17:47:40 +00:00
%token STAR
%token SLASH
%token PERCENT
2021-04-16 07:35:35 +00:00
%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
2021-04-18 22:29:54 +00:00
%parse-param { FILE* fp } { Stack *stack }
2021-04-16 21:40:28 +00:00
2021-04-16 07:35:35 +00:00
%left PLUS MINUS
%left BANG
%left LEFT_PAREN RIGHT_PAREN
%%
2021-04-21 02:00:18 +00:00
Program : TopLevelDeclarations
2021-04-16 07:35:35 +00:00
{
2021-04-18 20:14:50 +00:00
Node **declarations;
Node *declarationSequence;
uint32_t declarationCount;
2021-04-20 17:47:40 +00:00
declarations = GetNodes(stack, &declarationCount);
2021-04-18 20:14:50 +00:00
declarationSequence = MakeDeclarationSequenceNode(declarations, declarationCount);
PopStackFrame(stack);
2021-04-18 22:45:06 +00:00
rootNode = declarationSequence;
2021-04-16 07:35:35 +00:00
}
2021-04-21 02:00:18 +00:00
Type : VOID
{
$$ = MakeTypeNode(Void);
}
| INT
2021-04-18 20:14:50 +00:00
{
$$ = MakeTypeNode(Int);
}
| UINT
{
$$ = MakeTypeNode(UInt);
}
| FLOAT
{
$$ = MakeTypeNode(Float);
}
| DOUBLE
{
$$ = MakeTypeNode(Double);
}
| STRING
{
$$ = MakeTypeNode(String);
}
| BOOL
{
$$ = MakeTypeNode(Bool);
}
;
2021-04-16 21:40:28 +00:00
Identifier : ID
2021-04-16 07:35:35 +00:00
{
$$ = MakeIdentifierNode(yytext);
}
2021-04-16 21:40:28 +00:00
PrimaryExpression : Identifier
2021-04-16 07:35:35 +00:00
| NUMBER
{
$$ = MakeNumberNode(yytext);
}
| STRING
{
$$ = MakeStringNode(yytext);
}
| LEFT_PAREN Expression RIGHT_PAREN
2021-04-18 20:14:50 +00:00
{
$$ = $2;
}
2021-04-20 17:47:40 +00:00
| FunctionCallExpression
2021-04-16 07:35:35 +00:00
;
UnaryExpression : BANG Expression
{
$$ = MakeUnaryNode(Negate, $2);
}
BinaryExpression : Expression PLUS Expression
{
$$ = MakeBinaryNode(Add, $1, $3);
}
| Expression MINUS Expression
{
$$ = MakeBinaryNode(Subtract, $1, $3);
}
2021-04-20 17:47:40 +00:00
| Expression STAR Expression
{
$$ = MakeBinaryNode(Multiply, $1, $3);
}
2021-04-16 07:35:35 +00:00
Expression : PrimaryExpression
| UnaryExpression
| BinaryExpression
;
2021-04-16 21:40:28 +00:00
2021-04-18 20:30:50 +00:00
VariableDeclaration : Type Identifier
2021-04-18 20:14:50 +00:00
{
$$ = MakeDeclarationNode($1, $2);
}
AssignmentStatement : VariableDeclaration EQUAL Expression
{
$$ = MakeAssignmentNode($1, $3);
}
| Identifier EQUAL Expression
2021-04-16 21:40:28 +00:00
{
2021-04-18 20:14:50 +00:00
$$ = MakeAssignmentNode($1, $3);
2021-04-16 21:40:28 +00:00
}
2021-04-18 20:14:50 +00:00
2021-04-18 20:30:50 +00:00
ReturnStatement : RETURN Expression
{
$$ = MakeReturnStatementNode($2);
}
2021-04-21 02:00:18 +00:00
| RETURN
{
$$ = MakeReturnVoidStatementNode();
}
2021-04-18 20:30:50 +00:00
2021-04-20 17:47:40 +00:00
FunctionCallExpression : Identifier LEFT_PAREN Arguments RIGHT_PAREN
{
Node **arguments;
uint32_t argumentCount;
arguments = GetNodes(stack, &argumentCount);
$$ = MakeFunctionCallExpressionNode($1, MakeFunctionArgumentSequenceNode(arguments, argumentCount));
PopStackFrame(stack);
}
2021-04-18 20:14:50 +00:00
PartialStatement : AssignmentStatement
| VariableDeclaration
2021-04-20 17:47:40 +00:00
| FunctionCallExpression
2021-04-18 20:30:50 +00:00
| ReturnStatement
2021-04-18 20:14:50 +00:00
;
Statement : PartialStatement SEMICOLON;
2021-04-18 20:30:50 +00:00
Statements : Statement Statements
2021-04-16 21:40:28 +00:00
{
2021-04-20 17:47:40 +00:00
AddNode(stack, $1);
2021-04-16 21:40:28 +00:00
}
2021-04-18 20:30:50 +00:00
|
2021-04-16 21:40:28 +00:00
{
2021-04-18 20:30:50 +00:00
PushStackFrame(stack);
2021-04-16 21:40:28 +00:00
}
2021-04-18 20:14:50 +00:00
2021-04-20 17:47:40 +00:00
Arguments : PrimaryExpression COMMA Arguments
{
AddNode(stack, $1);
}
| PrimaryExpression
{
PushStackFrame(stack);
AddNode(stack, $1);
}
|
;
2021-04-21 02:00:18 +00:00
SignatureArguments : VariableDeclaration COMMA SignatureArguments
2021-04-20 01:18:45 +00:00
{
2021-04-20 17:47:40 +00:00
AddNode(stack, $1);
2021-04-20 01:18:45 +00:00
}
2021-04-18 20:14:50 +00:00
| VariableDeclaration
2021-04-20 01:18:45 +00:00
{
PushStackFrame(stack);
2021-04-20 17:47:40 +00:00
AddNode(stack, $1);
2021-04-20 01:18:45 +00:00
}
2021-04-20 17:47:40 +00:00
|
;
2021-04-18 20:14:50 +00:00
Body : LEFT_BRACE Statements RIGHT_BRACE
2021-04-16 21:40:28 +00:00
{
2021-04-18 20:30:50 +00:00
Node **statements;
Node *statementSequence;
uint32_t statementCount;
2021-04-20 17:47:40 +00:00
statements = GetNodes(stack, &statementCount);
2021-04-18 20:30:50 +00:00
statementSequence = MakeStatementSequenceNode(statements, statementCount);
$$ = MakeStatementSequenceNode(statements, statementCount);
PopStackFrame(stack);
2021-04-16 21:40:28 +00:00
}
2021-04-18 20:14:50 +00:00
2021-04-20 17:47:40 +00:00
FunctionSignature : Type Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN
2021-04-16 21:40:28 +00:00
{
2021-04-20 01:18:45 +00:00
Node **declarations;
uint32_t declarationCount;
2021-04-20 17:47:40 +00:00
declarations = GetNodes(stack, &declarationCount);
2021-04-20 01:18:45 +00:00
$$ = MakeFunctionSignatureNode($2, $1, MakeFunctionSignatureArgumentsNode(declarations, declarationCount));
PopStackFrame(stack);
2021-04-16 21:40:28 +00:00
}
2021-04-18 20:14:50 +00:00
FunctionDeclaration : FunctionSignature Body
2021-04-16 21:40:28 +00:00
{
2021-04-18 20:14:50 +00:00
$$ = MakeFunctionDeclarationNode($1, $2);
2021-04-16 21:40:28 +00:00
}
2021-04-21 02:00:18 +00:00
StructDeclaration : STRUCT Identifier LEFT_BRACE Declarations RIGHT_BRACE
2021-04-18 20:14:50 +00:00
{
Node **declarations;
Node *declarationSequence;
uint32_t declarationCount;
2021-04-16 21:40:28 +00:00
2021-04-20 17:47:40 +00:00
declarations = GetNodes(stack, &declarationCount);
2021-04-18 20:14:50 +00:00
declarationSequence = MakeDeclarationSequenceNode(declarations, declarationCount);
$$ = MakeStructDeclarationNode($2, declarationSequence);
2021-04-16 21:40:28 +00:00
2021-04-18 20:14:50 +00:00
PopStackFrame(stack);
}
2021-04-21 02:00:18 +00:00
Declaration : FunctionDeclaration
| VariableDeclaration SEMICOLON
2021-04-18 20:14:50 +00:00
;
Declarations : Declaration Declarations
2021-04-16 21:40:28 +00:00
{
2021-04-20 17:47:40 +00:00
AddNode(stack, $1);
2021-04-16 21:40:28 +00:00
}
|
{
2021-04-18 20:14:50 +00:00
PushStackFrame(stack);
2021-04-16 21:40:28 +00:00
}
2021-04-21 02:00:18 +00:00
TopLevelDeclaration : StructDeclaration;
TopLevelDeclarations : TopLevelDeclaration TopLevelDeclarations
{
AddNode(stack, $1);
}
|
{
PushStackFrame(stack);
}
2021-04-16 07:35:35 +00:00
%%