wraith-lang/generators/wraith.y

322 lines
9.9 KiB
Plaintext
Raw Normal View History

2021-04-18 22:29:54 +00:00
%code requires {
2021-04-29 19:42:51 +00:00
#include "../src/ast.h"
2021-04-18 22:29:54 +00:00
}
2021-04-16 21:40:28 +00:00
%{
#include <stdio.h>
2021-04-28 22:21:51 +00:00
#include "../src/ast.h"
2021-04-18 22:29:54 +00:00
2021-04-29 19:42:51 +00:00
void yyerror(FILE *fp, Node **pRootNode, 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-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-22 07:35:42 +00:00
%token STATIC
2021-04-24 19:59:30 +00:00
%token REFERENCE
2021-04-28 19:49:45 +00:00
%token ALLOC
2021-04-29 04:25:25 +00:00
%token IF
%token ELSE
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-29 19:42:51 +00:00
%parse-param { FILE* fp } { Node **pRootNode }
2021-04-16 21:40:28 +00:00
2021-04-23 00:19:35 +00:00
%define parse.error verbose
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-29 19:42:51 +00:00
*pRootNode = $1;
2021-04-16 07:35:35 +00:00
}
2021-04-24 19:59:30 +00:00
BaseType : VOID
2021-04-21 02:00:18 +00:00
{
2021-04-24 19:59:30 +00:00
$$ = MakePrimitiveTypeNode(Void);
2021-04-21 02:00:18 +00:00
}
| INT
2021-04-18 20:14:50 +00:00
{
2021-04-24 19:59:30 +00:00
$$ = MakePrimitiveTypeNode(Int);
2021-04-18 20:14:50 +00:00
}
| UINT
{
2021-04-24 19:59:30 +00:00
$$ = MakePrimitiveTypeNode(UInt);
2021-04-18 20:14:50 +00:00
}
| FLOAT
{
2021-04-24 19:59:30 +00:00
$$ = MakePrimitiveTypeNode(Float);
2021-04-18 20:14:50 +00:00
}
| DOUBLE
{
2021-04-24 19:59:30 +00:00
$$ = MakePrimitiveTypeNode(Double);
2021-04-18 20:14:50 +00:00
}
| STRING
{
2021-04-24 19:59:30 +00:00
$$ = MakePrimitiveTypeNode(String);
2021-04-18 20:14:50 +00:00
}
| BOOL
{
2021-04-24 19:59:30 +00:00
$$ = MakePrimitiveTypeNode(Bool);
}
| Identifier
{
$$ = MakeCustomTypeNode(yytext);
}
| REFERENCE LESS_THAN Type GREATER_THAN
{
$$ = MakeReferenceTypeNode($3);
2021-04-18 20:14:50 +00:00
}
;
2021-04-24 19:59:30 +00:00
Type : BaseType
{
$$ = MakeTypeNode($1);
}
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
2021-04-28 19:49:45 +00:00
HeapAllocation : ALLOC Type
{
$$ = MakeAllocNode($2);
}
2021-04-22 04:29:38 +00:00
AccessExpression : Identifier POINT AccessExpression
{
$$ = MakeAccessExpressionNode($1, $3);
}
| Identifier
{
$$ = $1;
}
PrimaryExpression : NUMBER
2021-04-16 07:35:35 +00:00
{
$$ = 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-22 04:29:38 +00:00
| AccessExpression
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-29 04:25:25 +00:00
| Expression LESS_THAN Expression
{
$$ = MakeBinaryNode(LessThan, $1, $3);
}
2021-04-16 07:35:35 +00:00
Expression : PrimaryExpression
| UnaryExpression
| BinaryExpression
2021-04-28 19:49:45 +00:00
| HeapAllocation
2021-04-16 07:35:35 +00:00
;
2021-04-16 21:40:28 +00:00
2021-04-23 04:16:37 +00:00
VariableDeclaration : Identifier COLON Type
2021-04-18 20:14:50 +00:00
{
2021-04-23 04:16:37 +00:00
$$ = MakeDeclarationNode($3, $1);
2021-04-18 20:14:50 +00:00
}
AssignmentStatement : VariableDeclaration EQUAL Expression
2021-04-22 04:29:38 +00:00
{
$$ = MakeAssignmentNode($1, $3);
}
| AccessExpression EQUAL Expression
2021-04-18 20:14:50 +00:00
{
$$ = 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-23 00:19:35 +00:00
FunctionCallExpression : AccessExpression LEFT_PAREN Arguments RIGHT_PAREN
2021-04-20 17:47:40 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = MakeFunctionCallExpressionNode($1, $3);
2021-04-20 17:47:40 +00:00
}
2021-04-23 00:19:35 +00:00
PartialStatement : FunctionCallExpression
| AssignmentStatement
2021-04-18 20:14:50 +00:00
| VariableDeclaration
2021-04-18 20:30:50 +00:00
| ReturnStatement
2021-04-18 20:14:50 +00:00
;
2021-04-29 04:25:25 +00:00
IfStatement : IF LEFT_PAREN Expression RIGHT_PAREN LEFT_BRACE Statements RIGHT_BRACE
{
2021-04-29 19:42:51 +00:00
$$ = MakeIfNode($3, $6);
}
2021-04-29 04:25:25 +00:00
2021-04-29 19:42:51 +00:00
Conditional : IfStatement
| IfStatement ELSE LEFT_BRACE Statements RIGHT_BRACE
{
$$ = MakeIfElseNode($1, $4);
2021-04-29 04:25:25 +00:00
}
Statement : PartialStatement SEMICOLON
2021-04-29 19:42:51 +00:00
| Conditional
2021-04-29 04:25:25 +00:00
;
2021-04-18 20:14:50 +00:00
2021-04-29 19:42:51 +00:00
Statements : Statement
2021-04-16 21:40:28 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = StartStatementSequenceNode($1);
2021-04-16 21:40:28 +00:00
}
2021-04-29 19:42:51 +00:00
| Statements Statement
2021-04-16 21:40:28 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = AddStatement($1, $2);
2021-04-16 21:40:28 +00:00
}
2021-04-18 20:14:50 +00:00
2021-04-29 19:42:51 +00:00
Arguments : PrimaryExpression
2021-04-20 17:47:40 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = StartFunctionArgumentSequenceNode($1);
2021-04-20 17:47:40 +00:00
}
2021-04-29 19:42:51 +00:00
| Arguments COMMA PrimaryExpression
2021-04-20 17:47:40 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = AddFunctionArgumentNode($1, $3);
2021-04-20 17:47:40 +00:00
}
|
2021-04-23 00:19:35 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = MakeEmptyFunctionArgumentSequenceNode();
2021-04-23 00:19:35 +00:00
}
2021-04-20 17:47:40 +00:00
2021-04-29 19:42:51 +00:00
SignatureArguments : VariableDeclaration
2021-04-20 01:18:45 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = StartFunctionSignatureArgumentsNode($1);
2021-04-20 01:18:45 +00:00
}
2021-04-29 19:42:51 +00:00
| SignatureArguments COMMA VariableDeclaration
2021-04-20 01:18:45 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = AddFunctionSignatureArgumentNode($1, $3);
2021-04-20 01:18:45 +00:00
}
2021-04-20 17:47:40 +00:00
|
2021-04-29 19:42:51 +00:00
{
$$ = MakeEmptyFunctionSignatureArgumentsNode();
}
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-29 19:42:51 +00:00
$$ = $2;
2021-04-16 21:40:28 +00:00
}
2021-04-18 20:14:50 +00:00
2021-04-23 04:16:37 +00:00
FunctionSignature : Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
2021-04-16 21:40:28 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = MakeFunctionSignatureNode($1, $6, $3, MakeFunctionModifiersNode(NULL, 0));
2021-04-22 07:35:42 +00:00
}
2021-04-23 04:16:37 +00:00
| STATIC Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
2021-04-22 07:35:42 +00:00
{
Node *modifier = MakeStaticNode();
2021-04-29 19:42:51 +00:00
$$ = MakeFunctionSignatureNode($2, $7, $4, MakeFunctionModifiersNode(&modifier, 1));
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
{
2021-04-29 19:42:51 +00:00
$$ = MakeStructDeclarationNode($2, $4);
2021-04-18 20:14:50 +00:00
}
2021-04-21 02:00:18 +00:00
Declaration : FunctionDeclaration
| VariableDeclaration SEMICOLON
2021-04-18 20:14:50 +00:00
;
2021-04-29 19:42:51 +00:00
Declarations : Declaration
2021-04-16 21:40:28 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = StartDeclarationSequenceNode($1);
2021-04-16 21:40:28 +00:00
}
2021-04-29 19:42:51 +00:00
| Declarations Declaration
2021-04-16 21:40:28 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = AddDeclarationNode($1, $2);
2021-04-16 21:40:28 +00:00
}
2021-04-21 02:00:18 +00:00
TopLevelDeclaration : StructDeclaration;
2021-04-29 19:42:51 +00:00
TopLevelDeclarations : TopLevelDeclaration
2021-04-21 02:00:18 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = StartDeclarationSequenceNode($1);
2021-04-21 02:00:18 +00:00
}
2021-04-29 19:42:51 +00:00
| TopLevelDeclarations TopLevelDeclaration
2021-04-21 02:00:18 +00:00
{
2021-04-29 19:42:51 +00:00
$$ = AddDeclarationNode($1, $2);
2021-04-21 02:00:18 +00:00
}
2021-04-16 07:35:35 +00:00
%%