]> git.mdlowis.com Git - proto/cerise-c.git/commitdiff
finished implementing test functions for operators
authorMike Lowis <mike.lowis@gentex.com>
Wed, 16 Oct 2024 17:18:31 +0000 (13:18 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Wed, 16 Oct 2024 17:18:31 +0000 (13:18 -0400)
cerise-c.m
cerise-c.rb

index cbca0d6b9cace854cffb5cdbe7cc692a798ddba5..4681534d74caba473e363d11a6a7211e8b3cb179 100644 (file)
@@ -1,6 +1,5 @@
 module TestSuite
 
-
 TestLiterals()
 {
     assert 42
@@ -131,15 +130,35 @@ TestNeqOps() {
 }
 
 TestLtOps(){
+    assert (1 < 2) == true
+    assert (1.0 < 2.0) == true
+    assert (2 < 1) == false
+    assert (2.0 < 1.0) == false
 }
 
 TestLtEqOps(){
+    assert (1 <= 2) == true
+    assert (2 <= 2) == true
+    assert (1.0 <= 2.0) == true
+    assert (2.0 <= 2.0) == true
+    assert (2 <= 1) == false
+    assert (2.0 <= 1.0) == false
 }
 
 TestGtOps(){
+    assert (1 > 2) == false
+    assert (1.0 > 2.0) == false
+    assert (2 > 1) == true
+    assert (2.0 > 1.0) == true
 }
 
 TestGtEqOps(){
+    assert (1 >= 2) == false
+    assert (1.0 >= 2.0) == false
+    assert (2 >= 1) == true
+    assert (1 >= 1) == true
+    assert (2.0 >= 1.0) == true
+    assert (1.0 >= 1.0) == true
 }
 
 TestIfBlocks(){
@@ -151,6 +170,11 @@ TestIfBlocks(){
     } else {
         assert true
     }
+    if (2 < 1) {
+        assert false
+    } else {
+        assert true
+    }
 }
 
 Main()
@@ -163,6 +187,10 @@ Main()
     TestArrayOps()
     TestHashOps()
     TestEqOps()
+    TestLtOps()
+    TestLtEqOps()
+    TestGtOps()
+    TestGtEqOps()
     TestNeqOps()
     TestIfBlocks()
 }
index 42a5df81c370a94544a1d367741f6b183e3e4795..2bceafe4ca21c867e423fec71d934895a1e4454b 100755 (executable)
@@ -2,13 +2,15 @@
 
 # TODO:
 #
+# * Fix type designation in symbol table
+# * Add Import/Export statements
+# * Add constant definitions
+# * Add type definitions
+#
 # * Add unary operators for integers
 # * Add type checking to operators on constants
 # * Add optimization of constant operators
 # * Add increment/decrement optimization
-# * Implement function calls
-# * Implement string constants
-# * Implement if statement
 # * Implement tail call optimization?
 
 require 'stringio'
@@ -139,18 +141,27 @@ class Parser
     @path = path
     @syms = SymTable.new
     @checker = TypeChecker.new(self)
+
+    syms.add_builtin(:Length, :func, [:any, :int], nil)
+
+
 #    syms.add_builtin(:void,   0, :type, :void)
 #    syms.add_builtin(:bool,   0, :type, :bool)
 #    syms.add_builtin(:int,    0, :type, :int)
 #    syms.add_builtin(:string, 0, :type, :string)
 #    syms.add_builtin(:float,  0, :type, :float)
 
-    syms.add_builtin(:Length, :func, [:any, :int], nil)
-    syms.add_builtin(:Error,  :func, [:any, :void], nil)
+#    syms.add_builtin(:Error,  :func, [:any, :void], nil)
 
     parse_file(path)
   end
 
+  def symbols()
+    @syms.globals.map do |k,v|
+      [k, {kind: v.kind, type: v.type}]
+    end.to_h
+  end
+
   def linenum(pos)
     @lex.linenum(pos)
   end
@@ -647,6 +658,10 @@ class SymTable
   def each(&block)
     @scopes.last.each(&block)
   end
+
+  def globals
+    @scopes[1]
+  end
 end
 
 
@@ -1369,8 +1384,22 @@ end
 # Parse and Analyze
 ##
 
-$parser  = Parser.new("cerise-c.m")
-$checker = TypeChecker.new($parser)
-$codegen = Codegen.new($parser)
-$codegen.output("cerise-c.c")
-`gcc -c -I. -O1 -o cerise-c.o #{"cerise-c.c"}`
+def compile(path)
+  out_path = path.sub(/\.[^.]+$/,'.c')
+  parser   = Parser.new(path)
+  checker  = TypeChecker.new(parser)
+  codegen  = Codegen.new(parser)
+  codegen.output(out_path)
+  `gcc -c -I. -O1 -o cerise-c.o #{out_path}`
+  parser.symbols
+end
+
+#$parser  = Parser.new("cerise-c.m")
+#$checker = TypeChecker.new($parser)
+#$codegen = Codegen.new($parser)
+#$codegen.output("cerise-c.c")
+#`gcc -c -I. -O1 -o cerise-c.o #{"cerise-c.c"}`
+
+#pp $parser
+
+pp compile("cerise-c.m")
\ No newline at end of file