Advertisement
Madmouse

Untitled

Sep 6th, 2020 (edited)
2,571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.09 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // THE SCOTCH-WARE LICENSE (Revision 0):
  3. // <aaronryool@gmail.com> wrote this file. As long as you retain this notice you
  4. // can do whatever you want with this stuff. If we meet some day, and you think
  5. // this stuff is worth it, you can buy me a shot of scotch in return
  6. ////////////////////////////////////////////////////////////////////////////////
  7.  
  8.  
  9. %require "3.2"
  10. %language "c++"
  11. %define api.namespace {yy}
  12. %define api.parser.class {parser}
  13. %define api.value.type variant
  14. %define api.token.constructor
  15. %defines
  16. %output "src/parser.yacc.cc"
  17.  
  18. %parse-param { yy::Lexer &lexer }  // Construct parser object with lexer
  19. %{
  20.  
  21.   #include <parser.lex.h>
  22.  
  23.   #undef yylex
  24.   #define yylex lexer.yylex  // Within bison's parse() we should invoke lexer.yylex(), not the global yylex()
  25.  
  26. %}
  27.  
  28. %code requires{
  29.     #include <ast/ast.h>
  30.    
  31.   namespace yy {
  32.     class Lexer;  // Generated by reflex with namespace=yy lexer=Lexer lex=yylex
  33.   }
  34. }
  35.  
  36.  
  37. %define api.token.prefix {TOK_}
  38.  
  39.  
  40.  
  41. %token LAND
  42. %token LOR
  43. %token LEQ
  44. %token LNEQ
  45. %token LTEQ
  46. %token GTEQ
  47.  
  48. %token POW
  49.  
  50.  
  51. %token RETURNS
  52. %token EXIT
  53. %token PRINT
  54. %token IF
  55. %token ELSE
  56. %token WHILE
  57. %token FOR
  58. %token TRUE
  59. %token FALSE
  60.  
  61.  
  62. %token IMPORT
  63. %token IRUN
  64. %token ISTART
  65. %token IROOT
  66.  
  67. %token TYPE_INT
  68. %token TYPE_FLOAT
  69. %token TYPE_STRING
  70. %token TYPE_NONE
  71. %token TYPE_TRUTH
  72. %token TYPE_LIST
  73. %token TYPE
  74.  
  75. %token <int> INT
  76. %token <double> FLOAT
  77. %token <std::string> STRING
  78. %token <std::string> VAR
  79.  
  80. %type <RootNode*> block
  81. %type <AstNode*> statement
  82.  
  83. %type <AstNode*> expression
  84. %type <ListNode*> list_expression
  85. %type <std::vector<AstNode*>> expression_list
  86. %type <Type> type_statement
  87. %type <bool> TRUTH
  88.  
  89.  
  90. %type <FunctionCallNode*> function_expression
  91. %type <FunctionNode*> function_statement
  92. %type <std::list<std::tuple<std::string, Type>>> input_list
  93.  
  94. %type <IfNode*> if_statement
  95. %type <AstNode*> loop_statement
  96. //%type <AstNode*> interactive
  97.  
  98.  
  99. %left ','
  100. %right '='
  101. %left LAND LOR
  102. %left LEQ LNEQ LTEQ GTEQ '<' '>'
  103.  
  104. %left '&' '|' '^'
  105. %left '+' '-'
  106.  
  107. %left '*' '/' '%'
  108. %left POW
  109. %right '(' ')'
  110.  
  111. %right ADDEQ
  112. %right SUBEQ
  113. %right POWEQ
  114. %right MULEQ
  115. %right DIVEQ
  116. %right MODEQ
  117. %right BANDEQ
  118. %right BOREQ
  119. %right BXOREQ
  120. %left ADDADD
  121. %left SUBSUB
  122.    
  123.  
  124. %precedence NEG   // negation
  125. %precedence NOT '!'
  126. %precedence TYPE
  127.  
  128. %right ISTART
  129. %left IRUN
  130.  
  131. %precedence IF
  132. %precedence ':'
  133. %right ';'
  134. %nonassoc NO_ELSE
  135. %nonassoc ELSE
  136.  
  137.  
  138. %%
  139.  
  140.  
  141. // root of AST, things actually get to run here
  142. run:
  143.     block                                   { if($1) $1->walk(); }
  144.     ;
  145.  
  146. // things attach to a block node, which is the root
  147. block:                                              { $$ = new RootNode; }
  148. /*
  149.     | IROOT                                         { $$ = new RootNode; current_scope = static_cast<RootNode*>($$); }
  150.     | block interactive                 {
  151.         if ($2 != NULL) {
  152.                 $$ = current_scope->attach_statement($2);
  153.                 // check state position, then act accoringly
  154.                 $$ = static_cast<RootNode*>(current_scope->state_start->walk());
  155.             }
  156.     }
  157. */
  158.     | block statement ';'           {
  159.             if ($2 != NULL) // cathces special case of empty statements not filtered by lex
  160.                 $$ = $1->attach_statement($2);
  161.             else $$ = $1;
  162.     }
  163.     | block if_statement                { $$ = $1->attach_statement($2); }
  164.     | block loop_statement          { $$ = $1->attach_statement($2); }
  165.     | block function_statement  { $$ = $1->attach_statement($2); }
  166.     ;
  167. /*
  168. interactive:
  169.     ISTART statement IRUN                       { $$ = $2; }
  170.     | ISTART statement ';' IRUN             { $$ = $2; }
  171.     | ISTART if_statement IRUN              { $$ = $2; }
  172.     | ISTART loop_statement IRUN            { $$ = $2; }
  173.     | ISTART function_statement IRUN    { $$ = $2; }
  174.     ;
  175. */
  176. type_statement:
  177.     TYPE_NONE               {$$ = NONE;}
  178.     | TYPE_INT          {$$ = INT;}
  179.     | TYPE_FLOAT        {$$ = FLOAT;}
  180.     | TYPE_STRING       {$$ = STRING;}
  181.     | TYPE_TRUTH        {$$ = TRUTH;}
  182.     | TYPE_LIST         {$$ = LIST;}
  183.     ;
  184.  
  185. // statements are branches of expressions
  186. statement:
  187.     expression                      { $$ = $1; }
  188.     | PRINT expression      { $$ = new BuiltinNode(PRINT, $2); }
  189.     | EXIT expression       { $$ = new BuiltinNode(EXIT, $2); }
  190.     | RETURNS expression    { $$ = new FunctionReturnNode($2); }
  191.     ;
  192.  
  193.  
  194. TRUTH:
  195.     FALSE               { $$ = 0; }
  196.     | TRUE          { $$ = 1; }
  197.     ;
  198.  
  199.  
  200. expression_list:
  201.     expression ',' expression                   { $$.push_back($1); $$.push_back($3);}
  202.     | expression_list ',' expression    { $$.insert($$.begin(), $1.begin(), $1.end()); $$.push_back($3); }
  203.     ;
  204.  
  205.  
  206. list_expression:
  207.     '[' expression_list ']'         { $$ = new ListNode($2); }
  208.     | '[' expression ']'                    {
  209.             $$ = new ListNode(std::vector<AstNode*>({$2}));
  210.     }
  211.     ;
  212.  
  213. expression:                                         {;} // ignore empty expressions
  214.     | INT                                                   { $$ = new IntNode($1); }
  215.     | TRUTH                                             { $$ = new TruthNode($1); }
  216.     | FLOAT                                             { $$ = new FloatNode($1); }
  217.     | STRING                                            { $$ = new StringNode($1); }
  218.     | TYPE_NONE                                     { $$ = new NoneNode(); }
  219.     | VAR '=' expression                    { $$ = new AssignmentNode($1, $3); }
  220.     | VAR                                                   { $$ = new SymbolReference($1); }
  221.     | list_expression                           { $$ = $1; }
  222.     | TYPE expression                       { $$ = new BuiltinNode(TYPE, $2); }
  223.     | NOT expression %prec NEG      { $$ = new MathNode(BNOT, NULL, $2); }
  224.     | '(' expression ')'                    { $$ = $2; }
  225.     | expression '+' expression     { $$ = new MathNode(ADD, $1, $3); }
  226.     | expression '-' expression     { $$ = new MathNode(SUB, $1, $3); }
  227.     | expression '*' expression     { $$ = new MathNode(MUL, $1, $3); }
  228.     | expression '/' expression     { $$ = new MathNode(DIV, $1, $3); }
  229.     | expression '%' expression     { $$ = new MathNode(MOD, $1, $3); }
  230.     | expression POW expression     { $$ = new MathNode(POW, $1, $3); }
  231.     | expression '&' expression     { $$ = new MathNode(BAND, $1, $3); }
  232.     | expression '|' expression     { $$ = new MathNode(BOR, $1, $3); }
  233.     | expression '^' expression     { $$ = new MathNode(BXOR, $1, $3); }
  234.     | expression LEQ expression     { $$ = new MathNode(LEQ, $1, $3); }
  235.     | expression LNEQ expression    { $$ = new MathNode(LNEQ, $1, $3); }
  236.     | expression LAND expression    { $$ = new MathNode(LAND, $1, $3); }
  237.     | expression LOR expression     { $$ = new MathNode(LOR, $1, $3); }
  238.     | expression '<' expression     { $$ = new MathNode(LT, $1, $3); }
  239.     | expression '>' expression     { $$ = new MathNode(GT, $1, $3); }
  240.     | expression LTEQ expression    { $$ = new MathNode(LTEQ, $1, $3); }
  241.     | expression GTEQ expression    { $$ = new MathNode(GTEQ, $1, $3); }
  242.     | function_expression                   { $$ = $1; }
  243.     | IF expression ':' expression ':' expression { $$ = new IfNode($2, $4, $6); }
  244. /*  | VAR   ADDADD                                  { $$ = ast_add_math_eq_operation(ADDADD, $1, NULL); }
  245.     | VAR SUBSUB                                    { $$ = ast_add_math_eq_operation(SUBSUB, $1, NULL); }
  246.     | ADDADD VAR                                    { $$ = ast_add_math_eq_operation(ADDADD, $2, NULL); }
  247.     | SUBSUB VAR                                    { $$ = ast_add_math_eq_operation(SUBSUB, $2, NULL); }
  248.     | VAR ADDEQ expression              { $$ = ast_add_math_eq_operation(ADDEQ, $1, $3); }
  249.     | VAR SUBEQ expression              { $$ = ast_add_math_eq_operation(SUBEQ, $1, $3); }
  250.     | VAR POWEQ expression              { $$ = ast_add_math_eq_operation(POWEQ, $1, $3); }
  251.     | VAR MULEQ expression              { $$ = ast_add_math_eq_operation(MULEQ, $1, $3); }
  252.     | VAR DIVEQ expression              { $$ = ast_add_math_eq_operation(DIVEQ, $1, $3); }
  253.     | VAR MODEQ expression              { $$ = ast_add_math_eq_operation(MODEQ, $1, $3); }
  254.     | VAR BANDEQ expression             { $$ = ast_add_math_eq_operation(BANDEQ, $1, $3); }
  255.     | VAR BOREQ expression              { $$ = ast_add_math_eq_operation(BOREQ, $1, $3); }
  256.     | VAR BXOREQ expression             { $$ = ast_add_math_eq_operation(BXOREQ, $1, $3); }
  257. */
  258.     ;
  259.  
  260. input_list:                                                     { $$; }
  261.     | type_statement VAR                                { $$.push_back(std::make_tuple($2, $1));}
  262.     | input_list ',' type_statement VAR { $$.splice($$.begin(), $1);$$.push_back(std::make_tuple($4, $3)); }
  263.     ;
  264.  
  265.  
  266. function_statement:
  267.     VAR type_statement '{' block '}' { $$ = new FunctionNode($1, {}, $2, $4); }
  268.     | VAR type_statement ':' expression ';' { $$ = new FunctionNode($1, {}, $2, $4); }
  269.     | VAR input_list ':' type_statement ':' expression ';' { $$ = new FunctionNode($1, $2, $4, $6); }
  270.     | VAR input_list ':' type_statement '{' block '}' { $$ = new FunctionNode($1, $2, $4, $6); }
  271.     | VAR input_list '{' block '}' { $$ = new FunctionNode($1, $2, NONE, $4); }
  272.     ;
  273.  
  274.  
  275.  
  276. function_expression:
  277.     VAR '(' expression_list ')' { $$ = new FunctionCallNode($1, $3); }
  278.     | VAR '(' expression ')'            {
  279.             $$ = new FunctionCallNode($1, std::vector<AstNode*>({$3}));
  280.     }
  281.     ;
  282.  
  283.  
  284. if_statement:
  285.     IF expression '{' block '}' %prec NO_ELSE                                   { $$ = new IfNode($2, $4, NULL); }
  286.     | IF expression ':' statement ';' %prec NO_ELSE                     { $$ = new IfNode($2, $4, NULL); }
  287.     | IF expression ':' statement ';' ELSE ':' statement ';'    { $$ = new IfNode($2, $4, $8); }
  288.     | IF expression '{' block '}' ELSE '{' block '}'                    { $$ = new IfNode($2, $4, $8); }
  289.     | IF expression '{' block '}' ELSE ':' statement ';'            { $$ = new IfNode($2, $4, $8); }
  290.     | IF expression ':' statement ';' ELSE '{' block '}'            { $$ = new IfNode($2, $4, $8); }
  291.    
  292.     ;
  293.  
  294. loop_statement:
  295.     WHILE expression '{' block '}' { $$ = new LoopNode($2, $4); }
  296.     | FOR expression ':' expression ':' expression '{' block '}' {
  297.             $$ = new ForNode($2, $4, $6, $8);
  298.     }
  299.     ;
  300.  
  301.  
  302. %%
  303.  
  304.  
  305. extern void parse_error(std::string msg);
  306. void yy::parser::error (const std::string& msg)
  307. {
  308.   parse_error(msg.c_str());
  309. }
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement