parser.ads (3350B)
1 with Ada.Containers.Vectors; 2 with Ada.Containers.Indefinite_Vectors; 3 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; 4 with Lexer; use Lexer; 5 6 package Parser is 7 8 type Node_Kind is 9 (NODE_PROGRAM, 10 NODE_FUNCTION_DECL, 11 NODE_STRUCT_DECL, 12 NODE_STRUCT_IMPL, 13 NODE_PARAM, 14 NODE_VAR_DECL, 15 NODE_VAR_ASSIGN, 16 NODE_FIELD_DECL, 17 NODE_FIELD_IMPL, 18 NODE_METHOD_DECL, 19 NODE_BLOCK, 20 NODE_CALL_EXPR, 21 NODE_RETURN, 22 NODE_BREAK, 23 NODE_CONTINUE, 24 NODE_INT_LITERAL, 25 NODE_FLOAT_LITERAL, 26 NODE_STRING_LITERAL, 27 NODE_BOOL_LITERAL, 28 NODE_IDENT, 29 NODE_TYPE, 30 NODE_BINARY_EXPR, 31 NODE_UNARY_EXPR, 32 NODE_EXPR_Stmt, 33 NODE_SUBSCRIPT_EXPR, 34 NODE_IF, 35 NODE_WHILE, 36 NODE_FOR, 37 NODE_EMPTY_Stmt, 38 NODE_UNKNOWN); 39 40 type Param is record 41 Name : Unbounded_String; 42 Kind : Unbounded_String; 43 end record; 44 45 package Param_Vectors is new 46 Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Param); 47 48 type Expr; 49 50 type Expr_Ref is access Expr; 51 52 package Expr_Vectors is new 53 Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Expr_Ref); 54 55 type Stmt_Kind is (Stmt_Call); 56 57 type Call_Stmt is record 58 Name : Unbounded_String; 59 Params : Expr_Vectors.Vector; 60 end record; 61 62 type Call_Expr is record 63 Callee : Unbounded_String; 64 Params : Expr_Vectors.Vector; 65 end record; 66 67 type Stmt (Kind : Stmt_Kind) is record 68 case Kind is 69 when Stmt_Call => 70 Call : Call_Stmt; 71 72 -- when Stmt_If => 73 -- Iff : If_Stmt; 74 75 -- when Stmt_While => 76 -- Whilee : While_Stmt; 77 78 -- when Stmt_Expr => 79 -- Expr : Expr_Stmt; 80 end case; 81 end record; 82 83 type Stmt_Ref is access Stmt; 84 85 package Stmt_Vectors is new 86 Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Stmt_Ref); 87 88 type Expr_Kind is (Expr_Call, String_Literal); 89 type Expr (Kind : Expr_Kind := Expr_Call) is record 90 case Kind is 91 when Expr_Call => 92 Call : Call_Expr; 93 94 when String_Literal => 95 Lit_Str : Unbounded_String; 96 end case; 97 end record; 98 99 type Func_Decl is record 100 Name : Unbounded_String; 101 Params : Param_Vectors.Vector; 102 Return_Kind : Unbounded_String; 103 Stmts : Stmt_Vectors.Vector; 104 end record; 105 106 type Decl_Kind is (Function_Kind); 107 108 type Decl (Kind : Decl_Kind) is record 109 case Kind is 110 when Function_Kind => 111 Func : Func_Decl; 112 113 -- when Struct_Kind => 114 -- Struct : Struct_Decl; 115 end case; 116 end record; 117 118 package Top_Level_Vectors is new 119 Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Decl); 120 121 type Parser is record 122 Source : Source_Ref; 123 File_Name : File_Name_Ref; 124 Tokens : Lexer.Token_Vector.Vector; 125 Top_Program : Top_Level_Vectors.Vector; 126 Pos : Positive; 127 end record; 128 129 function Init 130 (File_Name : File_Name_Ref; Source : Source_Ref; Tokens : Lexer.Token_Vector.Vector) 131 return Parser; 132 procedure Parse (P : in out Parser); 133 procedure Print_AST (P : Parser); 134 end Parser;