From 824a949c83db04f6388239abe9acd687ca65d0a4 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Thu, 13 Aug 2020 20:54:24 +0200 Subject: ... --- src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 | 3 +- src/core/src/tonkadur/fate/v1/parser/FateParser.g4 | 416 ++++++++++----------- 2 files changed, 210 insertions(+), 209 deletions(-) diff --git a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 index a4cdb76..ae3763d 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 @@ -30,7 +30,8 @@ DECLARE_ALIAS_TYPE_KW: DECLARE_DICT_TYPE_KW: L_PAREN ('declare'|'define'|'def')US('dict'|'struct')(US'type')? SEP+; DECLARE_EVENT_TYPE_KW: L_PAREN ('declare'|'define'|'def')US'event'(US'type')? SEP+; DECLARE_TEXT_EFFECT_KW: L_PAREN ('declare'|'define'|'def')US'text'US'effect' SEP+; -DECLARE_VARIABLE_KW: L_PAREN ('declare'|'define'|'def')US'var'('iable')? SEP+; +DECLARE_VARIABLE_KW: L_PAREN 'global' SEP+; +LOCAL_KW: L_PAREN 'local' SEP+; DEFINE_SEQUENCE_KW: L_PAREN ('declare'|'define'|'def')US'seq'('uence')? SEP+; DIVIDE_KW: L_PAREN ('divide'|'/'|'div') SEP+; DO_WHILE_KW: L_PAREN ('do'US'while') SEP+; diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index 312ffef..9d23055 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -150,39 +150,14 @@ first_level_fate_instr: } | DECLARE_VARIABLE_KW - type - WS+ - ); - - new_variable = - new Variable - ( - start_origin, - VariableScope.LOCAL, - ($type.result), - ($name.result) - ); - - WORLD.variables().add(new_variable); - } - - | IGNORE_ERROR_KW WORD WS+ first_level_fate_instr WS* R_PAREN - { - /* TODO: temporarily disable an compiler error category */ - } - - | DECLARE_VARIABLE_KW - scope=WORD - WS+ type WS+ name=new_reference_name WS* R_PAREN { - final Origin start_origin, scope_origin, type_origin; + final Origin start_origin, type_origin; final Variable new_variable; - VariableScope variable_scope; start_origin = CONTEXT.get_origin_at @@ -191,30 +166,10 @@ first_level_fate_instr: ($DECLARE_VARIABLE_KW.getCharPositionInLine()) ); - scope_origin = - CONTEXT.get_origin_at - ( - ($scope.getLine()), - ($scope.getCharPositionInLine()) - ); - - variable_scope = VariableScope.value_of(($scope.text)); - - if (variable_scope == null) - { - ErrorManager.handle - ( - new UnknownVariableScopeException(scope_origin, ($scope.text)) - ); - - variable_scope = VariableScope.ANY; - } - new_variable = new Variable ( start_origin, - variable_scope, ($type.result), ($name.result) ); @@ -222,6 +177,11 @@ first_level_fate_instr: WORLD.variables().add(new_variable); } + | IGNORE_ERROR_KW WORD WS+ first_level_fate_instr WS* R_PAREN + { + /* TODO: temporarily disable an compiler error category */ + } + | DECLARE_TEXT_EFFECT_KW params=type_list WS* @@ -280,81 +240,10 @@ first_level_fate_instr: WORLD.types().add(new_type); } - | DECLARE_SET_TYPE_KW parent=type WS+ new_reference_name WS* R_PAREN - { - final Origin start_origin; - final Type new_type; - - start_origin = - CONTEXT.get_origin_at - ( - ($DECLARE_SET_TYPE_KW.getLine()), - ($DECLARE_SET_TYPE_KW.getCharPositionInLine()) - ); - - new_type = - CollectionType.build - ( - start_origin, - ($parent.result), - true, - ($new_reference_name.result) - ); - - WORLD.types().add(new_type); - } - - | DECLARE_LIST_TYPE_KW parent=type WS+ new_reference_name WS* R_PAREN - { - final Origin start_origin; - final Type new_type; - - start_origin = - CONTEXT.get_origin_at - ( - ($DECLARE_LIST_TYPE_KW.getLine()), - ($DECLARE_LIST_TYPE_KW.getCharPositionInLine()) - ); - - new_type = - CollectionType.build - ( - start_origin, - ($parent.result), - false, - ($new_reference_name.result) - ); - - WORLD.types().add(new_type); - } - - | DECLARE_REF_TYPE_KW parent=type WS+ new_reference_name WS* R_PAREN - { - final Origin start_origin; - final Type new_type; - - start_origin = - CONTEXT.get_origin_at - ( - ($DECLARE_REF_TYPE_KW.getLine()), - ($DECLARE_REF_TYPE_KW.getCharPositionInLine()) - ); - - new_type = - new RefType - ( - start_origin, - ($parent.result), - ($new_reference_name.result) - ); - - WORLD.types().add(new_type); - } - | DECLARE_DICT_TYPE_KW new_reference_name WS* - typed_entry_list + variable_list WS* R_PAREN { @@ -367,7 +256,7 @@ first_level_fate_instr: for ( final TypedEntryList.TypedEntry te: - ($typed_entry_list.result).get_entries() + ($variable_list.result).get_entries() ) { field_types.put(te.get_name(), te.get_type()); @@ -483,48 +372,6 @@ first_level_fate_instr: CONTEXT.pop(); } - | DEFINE_MACRO_KW - new_reference_name - WS* - ( - L_PAREN WS+ typed_entry_list WS* R_PAREN - { - PARAMETERS=($typed_entry_list.result); - } - ) - WS* - general_fate_sequence - WS* - R_PAREN - { - final Origin origin; - final Macro new_macro; - - PARAMETERS = null; - - origin = - CONTEXT.get_origin_at - ( - ($DEFINE_MACRO_KW.getLine()), - ($DEFINE_MACRO_KW.getCharPositionInLine()) - ); - - new_macro = - new Macro - ( - origin, - new InstructionList - ( - origin, - ($general_fate_sequence.result) - ), - ($typed_entry_list.result), - ($new_reference_name.result) - ); - - WORLD.macros().add(new_macro); - } - | EXTENSION_FIRST_LEVEL_KW WORD WS+ general_fate_sequence WS* R_PAREN { final Origin origin; @@ -582,6 +429,52 @@ returns [Instruction result] ); } + | LOCAL_KW + type + WS+ + name=new_reference_name + WS* + R_PAREN + { + final Origin start_origin, type_origin; + final Variable new_variable; + final Map variable_map; + + start_origin = + ( + ($LOCAL_KW.getLine()), + ($LOCAL_KW.getCharPositionInLine()) + ); + + new_variable = + new Variable + ( + start_origin, + ($type.result), + ($name.result) + ); + + variable_map = LOCAL_VARIABLES.peekFirst(); + + if (variable_map.containsKey(($name.result))) + { + ErrorManager.handle + ( + new DuplicateLocalVariableException + ( + variable_map.get(($name.result)), + new_variable + ) + ); + } + else + { + variable_map.put(($name.result), new_variable); + } + + $result = new LocalVariable(new_variable); + } + | ADD_KW value WS+ value_reference WS* R_PAREN { $result = @@ -610,6 +503,19 @@ returns [Instruction result] ); } + | DONE_KW + { + $result = + new Done + ( + CONTEXT.get_origin_at + ( + ($DONE_KW.getLine()), + ($DONE_KW.getCharPositionInLine()) + ) + ); + } + | IGNORE_ERROR_KW WORD WS+ general_fate_instr WS* R_PAREN { /* TODO: temporarily disable an compiler error category */ @@ -828,12 +734,18 @@ returns [Instruction result] | FOR_EACH_KW value_reference WS+ new_reference_name { - Type collection_type, elem_type; + final Map variable_map; + final Variable new_variable; + final Type collection_type; + Type elem_type; + elem_type = Type.ANY; - if (PARAMETERS == null) + variable_map = local_variables.peekfirst(); + + if (local_variables == null) { - PARAMETERS = new TypedEntryList(); + local_variables = new typedentrylist(); } collection_type = ($value_reference.result).get_type(); @@ -861,25 +773,40 @@ returns [Instruction result] elem_type = Type.ANY; } - /* TODO: error if there is already a parameter with that name */ - PARAMETERS.add - ( - CONTEXT.get_origin_at + + new_variable = + new Variable ( - ($FOR_EACH_KW.getLine()), - ($FOR_EACH_KW.getCharPositionInLine()) - ), - elem_type, - ($new_reference_name.result) - ); + CONTEXT.get_origin_at + ( + ($FOR_EACH_KW.getLine()), + ($FOR_EACH_KW.getCharPositionInLine()) + ), + elem_type, + ($new_reference_name.result) + ); + + if (variable_map.containskey(($new_reference_name.result))) + { + ErrorManager.handle + ( + new DuplicateLocalVariableException + ( + variable_map.get(($new_reference_name.result)), + new_variable + ) + ); + } + else + { + variable_map.put(($new_reference_name.result), new_variable); + } } WS+ {BREAKABLE_LEVELS++;} general_fate_sequence {BREAKABLE_LEVELS--;} WS* R_PAREN { - PARAMETERS.remove(($new_reference_name.result)); - $result = new ForEach ( @@ -940,46 +867,86 @@ returns [Instruction result] ); } - | IMACRO_KW WORD WS+ value_list WS* R_PAREN + | VISIT_KW WORD WS+ value_list WS* R_PAREN { final Origin origin; - final Macro macro; + final String sequence_name; origin = CONTEXT.get_origin_at ( - ($IMACRO_KW.getLine()), - ($IMACRO_KW.getCharPositionInLine()) + ($VISIT_KW.getLine()), + ($VISIT_KW.getCharPositionInLine()) ); - macro = WORLD.macros().get(origin, ($WORD.text)); + sequence_name = ($WORD.text); - $result = - MacroCall.build + WORLD.add_sequence_use(origin, sequence_name, ($value_list.result)); + + $result = new SequenceCall(origin, sequence_name, ($value_list.result)); + } + + | VISIT_KW WORD WS* R_PAREN + { + final Origin origin; + final String sequence_name; + final List params; + + origin = + CONTEXT.get_origin_at ( - origin, - macro, - ($value_list.result) + ($VISIT_KW.getLine()), + ($VISIT_KW.getCharPositionInLine()) + ); + + params = new ArrayList(); + + sequence_name = ($WORD.text); + + WORLD.add_sequence_use(origin, sequence_name, params); + + $result = new SequenceCall(origin, sequence_name, params); + } + + | CONTINUE_AS_KW WORD WS+ value_list WS* R_PAREN + { + final Origin origin; + final String sequence_name; + + origin = + CONTEXT.get_origin_at + ( + ($CONTINUE_AS_KW.getLine()), + ($CONTINUE_AS_KW.getCharPositionInLine()) ); + + sequence_name = ($WORD.text); + + WORLD.add_sequence_use(origin, sequence_name, ($value_list.result)); + + $result = new SequenceJump(origin, sequence_name, ($value_list.result)); } - | SEQUENCE_KW WORD WS* R_PAREN + | CONTINUE_AS_KW WORD WS* R_PAREN { final Origin origin; final String sequence_name; + final List params; origin = CONTEXT.get_origin_at ( - ($SEQUENCE_KW.getLine()), - ($SEQUENCE_KW.getCharPositionInLine()) + ($CONTINUE_AS_KW.getLine()), + ($CONTINUE_AS_KW.getCharPositionInLine()) ); + params = new ArrayList(); + sequence_name = ($WORD.text); - WORLD.add_sequence_call(origin, sequence_name); + WORLD.add_sequence_use(origin, sequence_name, params); - $result = new SequenceCall(origin, sequence_name); + $result = new SequenceJump(origin, sequence_name, params); } | ASSERT_KW value WS+ paragraph WS* R_PAREN @@ -1602,11 +1569,11 @@ returns [List result] } ; -typed_entry_list -returns [TypedEntryList result] +variable_list +returns [VariableList result] @init { - $result = new TypedEntryList(); + $result = new VariableList(); } : ( @@ -1615,13 +1582,16 @@ returns [TypedEntryList result] { $result.add ( - CONTEXT.get_origin_at + new Variable ( - ($L_PAREN.getLine()), - ($L_PAREN.getCharPositionInLine()) - ), - ($type.result), - ($new_reference_name.result) + CONTEXT.get_origin_at + ( + ($L_PAREN.getLine()), + ($L_PAREN.getCharPositionInLine()) + ), + ($type.result), + ($new_reference_name.result) + ) ); } )* @@ -2342,6 +2312,37 @@ returns [Computation result] $result = ($boolean_expression.result); } + | LAMBDA_KW + L_PAREN WS+ variable_list WS* R_PAREN + { + final Map variable_map; + + variable_map = new HashMap(); + + variable_map.putAll(($variable_list.result).as_map()); + + LOCAL_VARIABLES.push(variable_map); + } + WS* + value + WS* + R_PAREN + { + $result = + new LambdaExpression + ( + CONTEXT.get_origin_at + ( + ($LAMBDA_KW.getLine()), + ($LAMBDA_KW.getCharPositionInLine()) + ), + ($variable_list.result), + ($value.result) + ); + + LOCAL_VARIABLES.pop(); + } + | math_expression { $result = ($math_expression.result); @@ -2423,25 +2424,22 @@ returns [Computation result] } } - | VMACRO_KW WORD WS+ value_list WS* R_PAREN + | EVAL_KW value_reference WS+ value_list WS* R_PAREN { final Origin origin; - final Macro macro; origin = CONTEXT.get_origin_at ( - ($VMACRO_KW.getLine()), - ($VMACRO_KW.getCharPositionInLine()) + ($EVAL_KW.getLine()), + ($EVAL_KW.getCharPositionInLine()) ); - macro = WORLD.macros().get(origin, ($WORD.text)); - $result = - MacroValueCall.build + LambdaEvaluation.build ( origin, - macro, + ($value_reference.result), ($value_list.result) ); } @@ -2498,6 +2496,8 @@ returns [Reference result] | ACCESS_KW value_reference WS+ value R_PAREN { $result = + Access.build + ( CONTEXT.get_origin_at ( ($ACCESS_KW.getLine()), -- cgit v1.2.3-70-g09d2