From 2ad2be250a68e907b308b120b934edcbfc99ae6e Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Sat, 20 May 2023 22:54:07 -0400 Subject: block scope and local variables (not really working) --- parser.d | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'parser.d') diff --git a/parser.d b/parser.d index ef1d736..978b218 100644 --- a/parser.d +++ b/parser.d @@ -13,6 +13,7 @@ enum FormType { SYMBOL, FUNC, DEF, + BLOCK, EOF, PARSE_ERROR @@ -79,6 +80,20 @@ class ParseError : Form { } } +class Block : Form { + + Form[] blockBody; + + this(int line) { + this.line = line; + this.type = FormType.BLOCK; + } + + void addToBody(Form f) { + this.blockBody ~= f; + } +} + class Cons : Form { Form head; @@ -376,6 +391,27 @@ class Parser { return func; } + Form parseBlock() { + Block block = new Block(line); + char next; + while(peekable()) { + next = peek(); + if (next == ')') { + break; + } + block.addToBody(parseForm()); + } + + if (!peekable()) { + return new ParseError("unterminated block", line); + } + + advance(); // consume closing paren + + return block; + + } + Form parseCons() { skipWhitespace(); @@ -395,6 +431,8 @@ class Parser { return parseFunc(); case "def": return parseDef(); + case "block": + return parseBlock(); default: break; } -- cgit v1.2.3