wraith-lang/generators/wraith.y

350 lines
11 KiB
Plaintext
Raw Normal View History

2021-04-18 22:29:54 +00:00
%code requires {
2021-04-28 22:21:51 +00:00
#include "../src/stack.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"
#include "../src/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-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-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-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-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-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-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
{
Node **arguments;
uint32_t argumentCount;
arguments = GetNodes(stack, &argumentCount);
$$ = MakeFunctionCallExpressionNode($1, MakeFunctionArgumentSequenceNode(arguments, argumentCount));
PopStackFrame(stack);
}
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
;
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-23 00:19:35 +00:00
{
PushStackFrame(stack);
}
2021-04-20 17:47:40 +00:00
;
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-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-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-23 04:16:37 +00:00
$$ = MakeFunctionSignatureNode($1, $6, MakeFunctionSignatureArgumentsNode(declarations, declarationCount), MakeFunctionModifiersNode(NULL, 0));
2021-04-22 07:35:42 +00:00
PopStackFrame(stack);
}
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 **declarations;
uint32_t declarationCount;
Node *modifier = MakeStaticNode();
declarations = GetNodes(stack, &declarationCount);
2021-04-23 04:16:37 +00:00
$$ = MakeFunctionSignatureNode($2, $7, MakeFunctionSignatureArgumentsNode(declarations, declarationCount), MakeFunctionModifiersNode(&modifier, 1));
2021-04-20 01:18:45 +00:00
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
%%