diff --git a/examples/arithmetics/arithmetic.tp2.json b/examples/arithmetics/arithmetic.tp2.json deleted file mode 100644 index 42a8420..0000000 --- a/examples/arithmetics/arithmetic.tp2.json +++ /dev/null @@ -1,101 +0,0 @@ -{ - "version": "2", - "lexer_rules": [ - { - "pattern": " |\\t|\\n", - "pass": false, - "name": null - }, - { - "pattern": "[0-9]+", - "pass": true, - "name": "[const]" - }, - { - "pattern": "\\+", - "pass": false, - "name": "[plus]" - }, - { - "pattern": "-", - "pass": false, - "name": "[minus]" - }, - { - "pattern": "\\*", - "pass": false, - "name": "[times]" - }, - { - "pattern": "/", - "pass": false, - "name": "[divided]" - }, - { - "pattern": "\\(", - "pass": false, - "name": "[open]" - }, - { - "pattern": "\\)|", - "pass": false, - "name": "[close]" - } - ], - "parser_rules": [ - { - "label": "{constant}", - "premise": "", - "conclusion": [ - {"type": "terminal", "parameters": {"id": "[const]"}} - ] - }, - { - "label": "{product}", - "premise": "", - "conclusion": [ - {"type": "variable", "parameters": {"id": ""}}, - {"type": "terminal", "parameters": {"id": "[times]"}}, - {"type": "variable", "parameters": {"id": ""}} - ] - }, - { - "label": "{quotient}", - "premise": "", - "conclusion": [ - {"type": "variable", "parameters": {"id": ""}}, - {"type": "terminal", "parameters": {"id": "[divided]"}}, - {"type": "variable", "parameters": {"id": ""}} - ] - }, - { - "label": "{sum}", - "premise": "", - "conclusion": [ - {"type": "variable", "parameters": {"id": ""}}, - {"type": "terminal", "parameters": {"id": "[plus]"}}, - {"type": "variable", "parameters": {"id": ""}} - ] - }, - { - "label": "{difference}", - "premise": "", - "conclusion": [ - {"type": "variable", "parameters": {"id": ""}}, - {"type": "terminal", "parameters": {"id": "[minus]"}}, - {"type": "variable", "parameters": {"id": ""}} - ] - }, - { - "label": "{priorised}", - "premise": "", - "conclusion": [ - {"type": "terminal", "parameters": {"id": "[open]"}}, - {"type": "variable", "parameters": {"id": ""}}, - {"type": "terminal", "parameters": {"id": "[close]"}} - ] - } - ], - "parser_start": "" -} - diff --git a/examples/arithmetics/arithmetics.tp2 b/examples/arithmetics/arithmetics.tp2 new file mode 100644 index 0000000..1cd74eb --- /dev/null +++ b/examples/arithmetics/arithmetics.tp2 @@ -0,0 +1,19 @@ +@1 + +[] : " |\t|\n" +[const] : "[0-9]+" : ~ +[plus] : "\+" +[minus] : "-" +[times] : "\*" +[divided] : "/" +[open] : "\(" +[close] : "\)" + +{constant} : : [const] +{product} : : [times] +{quotient} : : [divided] +{sum} : : [plus] +{difference} : : [minus] +{priorised} : : [open] [close] + + diff --git a/examples/arithmetics/eval b/examples/arithmetics/eval index 25c82c0..480ab69 100755 --- a/examples/arithmetics/eval +++ b/examples/arithmetics/eval @@ -6,17 +6,17 @@ import json as _json def evaluate(node): # _sys.stderr.write("eval: %s\n" % _json.dumps(node)) - if (node["label"] == "{constant}"): + if (node["label"] == "constant"): return int(node["value"][0]["data"]["value"]) - elif (node["label"] == "{sum}"): + elif (node["label"] == "sum"): return (evaluate(node["children"][0]) + evaluate(node["children"][1])) - elif (node["label"] == "{difference}"): + elif (node["label"] == "difference"): return (evaluate(node["children"][0]) - evaluate(node["children"][1])) - elif (node["label"] == "{product}"): + elif (node["label"] == "product"): return (evaluate(node["children"][0]) * evaluate(node["children"][1])) - elif (node["label"] == "{quotient}"): + elif (node["label"] == "quotient"): return (evaluate(node["children"][0]) / evaluate(node["children"][1])) - elif (node["label"] == "{priorised}"): + elif (node["label"] == "priorised"): return evaluate(node["children"][0]) else: raise ValueError("unhandled label: " + node["label"]) @@ -26,7 +26,7 @@ def main(): content = _sys.stdin.read() node = _json.loads(content) value = evaluate(node) - _sys.stdout.write(str(value) + "\n") + _sys.stdout.write(_json.dumps(value) + "\n") main() diff --git a/examples/arithmetics/readme.md b/examples/arithmetics/readme.md new file mode 100644 index 0000000..452d241 --- /dev/null +++ b/examples/arithmetics/readme.md @@ -0,0 +1,5 @@ +## Usage + +- `cat examples/arithmetics/arithmetics.tp2 | tools/conv > /tmp/arithmetics.tp2.json` +- `echo '2+3' | build/type2 /tmp/arithmetics.tp2.json` +- `echo '(2+3)*5' | build/type2 /tmp/arithmetics.tp2.json | examples/arithmetics/eval`