wraith-lang/wraith.y

248 lines
6.9 KiB
Plaintext
Raw Normal View History

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-16 21:40:28 +00:00
#define YYSTYPE struct Node*
void yyerror(FILE *fp, char *s)
{
fprintf (stderr, "%s\n", s);
}
2021-04-18 20:14:50 +00:00
Stack *stack;
#define YYDEBUG 1
int yydebug=1;
2021-04-16 21:40:28 +00:00
%}
2021-04-16 07:35:35 +00:00
%token NUMBER
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-16 21:40:28 +00:00
%token ID
%token STRING_LITERAL
2021-04-16 07:35:35 +00:00
%token PLUS
%token MINUS
%token MULTIPLY
%token DIVIDE
%token MOD
%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-16 21:40:28 +00:00
%parse-param { FILE* fp }
2021-04-16 07:35:35 +00:00
%left PLUS MINUS
%left BANG
%left LEFT_PAREN RIGHT_PAREN
%%
2021-04-18 20:14:50 +00:00
Program : Declarations
2021-04-16 07:35:35 +00:00
{
2021-04-18 20:14:50 +00:00
Node **declarations;
Node *declarationSequence;
uint32_t declarationCount;
declarations = GetDeclarations(stack, &declarationCount);
declarationSequence = MakeDeclarationSequenceNode(declarations, declarationCount);
PopStackFrame(stack);
PrintTree(declarationSequence, 0);
2021-04-16 07:35:35 +00:00
}
2021-04-18 20:14:50 +00:00
Type : INT
{
$$ = 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-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);
}
Expression : PrimaryExpression
| UnaryExpression
| BinaryExpression
;
2021-04-16 21:40:28 +00:00
2021-04-18 20:14:50 +00:00
VariableDeclaration : Type Identifier SEMICOLON
{
$$ = 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
PartialStatement : AssignmentStatement
| VariableDeclaration
;
Statement : PartialStatement SEMICOLON;
Statements : Statement
2021-04-16 21:40:28 +00:00
{
2021-04-18 20:14:50 +00:00
Node **statements;
uint32_t statementCount;
AddStatement(stack, $1);
statements = GetStatements(stack, &statementCount);
$$ = MakeStatementSequenceNode(statements, statementCount);
PopStackFrame(stack);
2021-04-16 21:40:28 +00:00
}
2021-04-18 20:14:50 +00:00
| Statement Statements
2021-04-16 21:40:28 +00:00
{
2021-04-18 20:14:50 +00:00
AddStatement(stack, $2);
2021-04-16 21:40:28 +00:00
}
2021-04-18 20:14:50 +00:00
Arguments : Arguments COMMA VariableDeclaration
| VariableDeclaration
;
Body : LEFT_BRACE Statements RIGHT_BRACE
2021-04-16 21:40:28 +00:00
{
2021-04-18 20:14:50 +00:00
$$ = $2;
2021-04-16 21:40:28 +00:00
}
2021-04-18 20:14:50 +00:00
FunctionSignature : Type Identifier LEFT_PAREN Arguments RIGHT_PAREN
2021-04-16 21:40:28 +00:00
{
2021-04-18 20:14:50 +00:00
$$ = MakeFunctionSignatureNode($2, $1, $4);
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-18 20:14:50 +00:00
VariableDeclarations : VariableDeclaration VariableDeclarations
2021-04-16 21:40:28 +00:00
{
2021-04-18 20:14:50 +00:00
AddDeclaration(stack, $1);
2021-04-16 21:40:28 +00:00
}
2021-04-18 20:14:50 +00:00
|
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-18 20:14:50 +00:00
StructDeclaration : STRUCT Identifier LEFT_BRACE VariableDeclarations RIGHT_BRACE
{
Node **declarations;
Node *declarationSequence;
uint32_t declarationCount;
2021-04-16 21:40:28 +00:00
2021-04-18 20:14:50 +00:00
declarations = GetDeclarations(stack, &declarationCount);
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);
}
Declaration : StructDeclaration
| FunctionDeclaration
;
Declarations : Declaration Declarations
2021-04-16 21:40:28 +00:00
{
2021-04-18 20:14:50 +00:00
AddDeclaration(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-16 07:35:35 +00:00
%%
#include "lex.yy.c"
2021-04-16 21:40:28 +00:00
int main(int argc, char *argv[])
2021-04-16 07:35:35 +00:00
{
2021-04-16 21:40:28 +00:00
if (argc < 2)
{
printf("Please provide a file.\n");
return 1;
}
2021-04-16 07:35:35 +00:00
2021-04-18 20:14:50 +00:00
stack = CreateStack();
2021-04-16 21:40:28 +00:00
FILE *fp = fopen(argv[1], "r");
yyin = fp;
yyparse(fp);
fclose(fp);
2021-04-18 20:14:50 +00:00
2021-04-16 21:40:28 +00:00
return 0;
2021-04-16 07:35:35 +00:00
}