From 38dc63a67879a42f208b5642a8590e1192e8e2e5 Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Sat, 20 May 2023 18:58:51 -0400 Subject: global variables --- parser.d | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'parser.d') 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; } -- cgit v1.2.3