]> git.mdlowis.com Git - proto/sclpl-rb.git/commitdiff
added symbol table class
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 5 Feb 2020 21:50:30 +0000 (16:50 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 5 Feb 2020 21:50:30 +0000 (16:50 -0500)
compile.rb

index a6eb88cd158a3f92202be107c9c75bc03d3fbf11..7a5ef8e130dd1e3196f7c531548d62ae2fa14156 100755 (executable)
@@ -54,6 +54,29 @@ BuiltinSyms = {
   },
 }
 
+class Symtable
+  def initialize
+    @builtins = BuiltinSyms
+    @scopes = [{}]
+  end
+
+  def []=(k,v)
+    @scopes.last[k] = v
+  end
+
+  def [](k)
+    (@scopes.map{|h| h[k] }.compact.last || BuiltinSyms[k])
+  end
+
+  def scope_start
+    @scopes.push({})
+  end
+
+  def scope_stop
+    @scopes.pop()
+  end
+end
+
 class Lexer
   Tok = Struct.new(:text, :file, :pos, :type)
   SPACE = /([ \t\v\n\r]+|#.*\n)/
@@ -527,6 +550,7 @@ class Package
     @declares = {}
     @definitions = {}
     add_file(path)
+    type_check
   end
 
   def add_file(path)
@@ -536,10 +560,6 @@ class Package
     @definitions = parse.definitions()
   end
 
-  def add_files(paths)
-    paths.each {|p| add_file(p) }
-  end
-
   def dump
     pp({
         name: @name,
@@ -547,6 +567,9 @@ class Package
         defines: @definitions
     })
   end
+
+  def type_check
+  end
 end
 
 pkg = Package.new("example.src")