forked from cosmonaut/wraith-lang
92 lines
1.9 KiB
Plaintext
92 lines
1.9 KiB
Plaintext
|
%token NUMBER
|
||
|
%token ID
|
||
|
%token STRING
|
||
|
%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
|
||
|
|
||
|
%left PLUS MINUS
|
||
|
%left BANG
|
||
|
%left LEFT_PAREN RIGHT_PAREN
|
||
|
|
||
|
%{
|
||
|
#include "ast.h"
|
||
|
#define YYSTYPE struct Node*
|
||
|
%}
|
||
|
|
||
|
%%
|
||
|
Main : Expression NEWLINE
|
||
|
{
|
||
|
PrintTree($1);
|
||
|
}
|
||
|
|
||
|
PrimaryExpression : ID
|
||
|
{
|
||
|
$$ = MakeIdentifierNode(yytext);
|
||
|
}
|
||
|
| NUMBER
|
||
|
{
|
||
|
$$ = MakeNumberNode(yytext);
|
||
|
}
|
||
|
| STRING
|
||
|
{
|
||
|
$$ = MakeStringNode(yytext);
|
||
|
}
|
||
|
| LEFT_PAREN Expression RIGHT_PAREN
|
||
|
;
|
||
|
|
||
|
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
|
||
|
;
|
||
|
%%
|
||
|
|
||
|
#include "lex.yy.c"
|
||
|
|
||
|
/* yacc error handler */
|
||
|
void yyerror(char *s)
|
||
|
{
|
||
|
fprintf (stderr, "%s\n", s);
|
||
|
}
|
||
|
|
||
|
int main(void)
|
||
|
{
|
||
|
return yyparse();
|
||
|
}
|