aboutsummaryrefslogtreecommitdiff
path: root/parser.d
diff options
context:
space:
mode:
authorBen Winston2023-05-20 18:58:51 -0400
committerBen Winston2023-05-20 18:58:51 -0400
commit38dc63a67879a42f208b5642a8590e1192e8e2e5 (patch)
treee36a71c752d8d0c89e343ef02e295115d3e19ce7 /parser.d
parent0c70372774297272dd14133d48e40e9a3624420a (diff)
global variables
Diffstat (limited to 'parser.d')
-rw-r--r--parser.d33
1 files changed, 33 insertions, 0 deletions
diff --git a/parser.d b/parser.d
index 20231c9..ef1d736 100644
--- a/parser.d
+++ b/parser.d
@@ -12,6 +12,7 @@ enum FormType {
NIL,
SYMBOL,
FUNC,
+ DEF,
EOF,
PARSE_ERROR
@@ -134,6 +135,19 @@ class Func : Form {
}
}
+class Def : Form {
+
+ Symbol name;
+ Form val;
+
+ this(Symbol name, Form val, int line) {
+ this.name = name;
+ this.val = val;
+ this.line = line;
+ this.type = FormType.DEF;
+ }
+}
+
class Atom : Form {
Value value;
@@ -310,6 +324,23 @@ class Parser {
return new Atom(to!double(to!string(acc)), line);
}
+ Form parseDef() {
+ // we've parsed `def`, but not the symbol yet
+ Form sym = parseForm();
+ if (sym.type != FormType.SYMBOL) {
+ return new ParseError("func definitions expect a symbol name", line);
+ }
+
+ // get the value
+ Form val = parseForm();
+
+ Def def = new Def(cast(Symbol)sym, val, line);
+
+ advance(); // closing paren
+
+ return def;
+ }
+
Form parseFunc() {
// we've parsed `func`, but not the symbol yet
Form sym = parseForm();
@@ -362,6 +393,8 @@ class Parser {
switch (s.name) {
case "func":
return parseFunc();
+ case "def":
+ return parseDef();
default:
break;
}