From 733223fc2baa87db7ff2fd23b2d7b473dc41a06b Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 30 Jul 2019 13:47:23 -0400 Subject: [PATCH] added bare minimum constant parsing (only ints) --- compile.rb | 37 +++++++++++++++++++++++++++++++++++-- example.src | 2 +- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/compile.rb b/compile.rb index db7151c..aa3186a 100755 --- a/compile.rb +++ b/compile.rb @@ -145,8 +145,23 @@ class Lexer end class Parser + LEVELS = { + none: 0, + assign: 1, + or: 2, + and: 3, + equality: 4, + compare: 5, + term: 6, + factor: 7, + unary: 8, + call: 9, + primary: 10, + } + def initialize(path) @lex = Lexer.new(path) + @prev = nil @next = nil end @@ -179,15 +194,19 @@ class Parser defs end + private + def topdef(defs) dectype = declaration(defs) if dectype == :fun then expect("{") + value = expression() expect("}") else expect("=") - expect(:int) + value = expression() end + pp value end def declaration(syms) @@ -223,7 +242,20 @@ class Parser end end - private + def parseLevel(level) + end + + def expression() + constant() + end + + def constant() + if (peek().type == :int) + AST::Value.new(Type::Int, consume().text.to_i) + else + error("not a valid constant") + end + end def error(str) raise str @@ -240,6 +272,7 @@ class Parser def accept(type) if (matches(type)) + @prev = @next @next = nil true else diff --git a/example.src b/example.src index 5bc65b9..c5b48c1 100644 --- a/example.src +++ b/example.src @@ -7,5 +7,5 @@ provides { let foo int = 42 fun main(args string[]) int { - + 42 } -- 2.52.0