Commits

Paweł Wieczorek committed 5edaa96

Declaring routines with arguments, in WHILE

  • Participants
  • Parent commits 1889c3b
  • Branches very_simple_while_compiler

Comments (0)

Files changed (5)

File source/Compiler/WhileX86Compiler.ml

         | AST.CMD_Read variable ->
             [variable]
 
-    let collect_all_variables_from_declaration (AST.DECL_Procedure (identifier, command)) =
+    let collect_all_variables_from_declaration (AST.DECL_Procedure (identifier, formal_arguments, command)) =
         collect_all_variables_from_command command
 
     let rec collect_all_exceptions_from_command = function
         | _ ->
             []
 
-    let collect_all_exceptions_from_declaration (AST.DECL_Procedure (identifier, command)) =
+    let collect_all_exceptions_from_declaration (AST.DECL_Procedure (identifier, formal_arguments, command)) =
         collect_all_exceptions_from_command command
 
 end
 
     let compute_symbol_table declarations =
         let hashtbl = Hashtbl.create 7 in
-        let from_declaration (AST.DECL_Procedure (identifier, command)) =
+        let from_declaration (AST.DECL_Procedure (identifier, formal_arguments, command)) =
             let symbol = create_symbol identifier in
             Hashtbl.replace hashtbl identifier symbol
             in
         let compiled_body      = Helpers.frame_wrap frame_leave_symbol compiled_command in
         emit_label symbol :: compiled_body
 
-    let compile_declaration context (AST.DECL_Procedure (identifier, command)) =
+    let compile_declaration context (AST.DECL_Procedure (identifier, formal_arguments, command)) =
         let symbol           = Context.lookup_for_symbol context identifier in
         let compiled_command = compile_procedure_command context symbol command in
         Shrinker.shrink compiled_command

File source/Lang/While/AST.ml

     | CMD_Try of command * identifier * command
 
 type declaration
-    = DECL_Procedure of identifier * command
+    = DECL_Procedure of identifier * variable list * command
 
 type program
     = PROGRAM of declaration list

File source/Lang/While/Eval.ml

     let rec eval_program store io_driver = function
         | PROGRAM decls ->
             let analyze_declaration decls = function
-                | DECL_Procedure (ident, command) ->
+                | DECL_Procedure (ident, formal_arguments, command) ->
                     Hashtbl.replace decls ident command; decls
 
             in let analyzed_declarations = List.fold_left analyze_declaration (Hashtbl.create 128) decls

File source/Lang/While/Parser.mly

     { $1 }
     ;
 
+variable_list
+    : variable COMA variable_list
+    { $1 :: $3 }
+
+    | variable
+    { [$1] }
+    ;
+
+
 argument_list
     : arithmetic_expression COMA argument_list
     { $1 :: $3 }
 declaration
     : PROCEDURE identifier LPARENT RPARENT
         commands 
-    { DECL_Procedure ($2, $5) } 
+    { DECL_Procedure ($2, [], $5) } 
+
+    | PROCEDURE identifier LPARENT variable_list RPARENT
+        commands 
+    { DECL_Procedure ($2, $4, $6) } 
+    ;
 
 declarations
     : declaration 
 
 parse
     : program EOF
-    { raise Exit }
+    { $1 }
 %% 
 

File source/Lang/While/PrettyPrinter.ml

         psp_group (List.map paint_declaration decls)
 
 and paint_declaration = function
-    | DECL_Procedure (ident, command) -> psp_group
+    | DECL_Procedure (ident, formal_arguments, command) -> psp_group
         [ psp_keyword "procedure"
         ; psp_identifier ident
-        ; psp_word "()"
+        ; psp_list_map psp_std_bracket (psp_syntax ",") psp_variable formal_arguments
         ; psp_break
         ; psp_syntax "{"
         ; psp_break