diff options
Diffstat (limited to 'lib/qsp_instruction.mly')
-rw-r--r-- | lib/qsp_instruction.mly | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/lib/qsp_instruction.mly b/lib/qsp_instruction.mly new file mode 100644 index 0000000..cfc6124 --- /dev/null +++ b/lib/qsp_instruction.mly @@ -0,0 +1,93 @@ +%% + +optionnal_delimited(opening, X, closing): + | v = delimited(opening, X, closing) { v } + | v = X { v } + +argument(X): + | a = optionnal_delimited(L_PAREN, arguments(X), R_PAREN) { a } + | a = X { [ a ] } + +(** At the opposite of an expression, an instruction does not return anything. *) +%public instruction: + | s = single_instruction { s } + +(** Action like act or if in a single line *) +%public inline_action: + | a = onliner(ACT) + { let loc, label, statements, _, _ = a in + Analyzer.Instruction.act loc ~label statements + } + | a = onliner(IF) + else_opt = preceded(ELSE, instruction)? + { let loc, expression, statements, loc_s, _body = a in + let elifs = [] + and else_ = Option.to_list else_opt in + Analyzer.Instruction.if_ + loc + (loc_s, expression, statements) + ~elifs + ~else_ + } + +single_instruction: + | expr = expression + { + Analyzer.Instruction.expression expr + } + | e = let_assignation { e } + | k = keyword + arg = argument(expression) + { + Analyzer.Instruction.call $loc k arg + } + +keyword: + | STAR k = KEYWORD { "*" ^ k } + | k = KEYWORD { k } + +let_assignation: + | assignation + variable = variable + op = assignation_operator + value = expression + { + Analyzer.Instruction.assign $loc variable op value + } + +%inline assignation: + | + | LET + | SET {} + +assignation_operator: + | EQUAL { T.Eq' } + | INCR { T.Inc } + | DECR { T.Decr } + +inline_instruction: + | hd = inline_instruction + tl = single_instruction + AMPERSAND+ + { tl :: hd } + | + { [] } + +final_inline_instruction: + | hd = inline_instruction + tl = instruction + | hd = inline_instruction + tl = inline_action + { tl :: hd } + | hd = inline_instruction + COMMENT + { (Analyzer.Instruction.comment $loc) :: hd } + | hd = inline_instruction + { hd } + +onliner(TOKEN): + | TOKEN + e = expression + COLUMN + s = rev (final_inline_instruction) + { $loc, e, s, $loc(s), None } |