]> git.mdlowis.com Git - proto/aas.git/commitdiff
fixed up binary ops
authorMike Lowis <mike.lowis@gentex.com>
Mon, 13 Nov 2023 21:19:39 +0000 (16:19 -0500)
committerMike Lowis <mike.lowis@gentex.com>
Mon, 13 Nov 2023 21:19:39 +0000 (16:19 -0500)
aas.rb
cerise.m
cerise.rb

diff --git a/aas.rb b/aas.rb
index 672cff8d06f52bbfcdd211d1d2d07063820dcbaa..0d06a056fbd440429f2dba443173e85360cf0506 100755 (executable)
--- a/aas.rb
+++ b/aas.rb
@@ -74,15 +74,15 @@ module Targets
       end
 
       def divi
-        emit "popq      %rax"
         emit "popq      %rbx"
+        emit "popq      %rax"
         emit "idivq     %rbx, %rax"
         emit "pushq     %rax"
       end
 
       def modi
-        emit "popq      %rax"
         emit "popq      %rbx"
+        emit "popq      %rax"
         emit "idivq     %rbx, %rax"
         emit "pushq     %rdx"
       end
index fc7e1bb1bffa13bce98f296af7855ec6e7b179c4..9c3468fb8782610208121d7e9ab05a4883a59282 100644 (file)
--- a/cerise.m
+++ b/cerise.m
@@ -1,4 +1,4 @@
 main()
 {
-    return 43 % 2
+    return 42 * 2
 }
index 6daa8e5957ec003fa368020dc6c4e960f3144721..513eb6b22f9790dd1dd39b787badc094731664bb 100755 (executable)
--- a/cerise.rb
+++ b/cerise.rb
@@ -1,5 +1,16 @@
 #!/usr/bin/env ruby
 
+# TODO:
+#
+# * 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'
 require 'strscan'
 
@@ -694,8 +705,22 @@ class Parser
   end
 
   def simple_expr()
-    term()
+    # Handle unary ops first
+    if matches_any(["+", "-"])
+      op = consume()
+      left = term()
+      left = IR::Op.new(op.location, nil, op.text, left, nil)
+    else
+      left = term()
+    end
 
+    # now look for binary ops
+    while matches_any(["+", "-", :or])
+      op = consume()
+      right = term();
+      left = IR::Op.new(location, nil, op.text, left, right)
+    end
+    left
 # 269     /* first term and +/- unary ops */
 # 270     if (matches_oneof(p, (int[]){'+', '-', 0}))
 # 271     {
@@ -766,9 +791,9 @@ module Codegen
     elsif v.is_a? IR::Const then
       emit_const(v)
     elsif v.is_a? IR::Op then
-      emit_op(v)
+      emit_binop(v)
     else
-      raise "not implemented"
+      raise "code emitting of #{v.class} not implemented"
     end
   end
 
@@ -783,10 +808,14 @@ module Codegen
     end
   end
 
-  def self.emit_op(v)
-    emit(v.right) if v.right
+  def self.emit_binop(v)
     emit(v.left)
+    emit(v.right)
     case v.op
+    when "+"
+      puts "    addi"
+    when "-"
+      puts "    subi"
     when "*"
       puts "    muli"
     when "/"