]> git.mdlowis.com Git - proto/aas.git/commitdiff
fixed up comparison operators
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 21 Nov 2023 03:44:14 +0000 (22:44 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 21 Nov 2023 03:44:14 +0000 (22:44 -0500)
aas.rb
cerise.m
cerise.rb

diff --git a/aas.rb b/aas.rb
index 3ff42f64e9a2a6d3e6d9087079bdd7774dcaa88e..c3307bae849d6d6a24c09280606428418625570b 100755 (executable)
--- a/aas.rb
+++ b/aas.rb
@@ -111,7 +111,7 @@ module Targets
       def eqi
         emit "popq      %rax"
         emit "popq      %rbx"
-        emit "cmpq      %rbx, %rax"
+        emit "cmpq      %rax, %rbx"
         emit "sete      %al"
         emit "movzbq    %al, %rax"
         emit "pushq     %rax"
@@ -121,7 +121,7 @@ module Targets
       def nei
         emit "popq      %rax"
         emit "popq      %rbx"
-        emit "cmpq      %rbx, %rax"
+        emit "cmpq      %rax, %rbx"
         emit "setne     %al"
         emit "movzbq    %al, %rax"
         emit "pushq     %rax"
@@ -131,7 +131,7 @@ module Targets
       def lti
         emit "popq      %rax"
         emit "popq      %rbx"
-        emit "cmpq      %rbx, %rax"
+        emit "cmpq      %rax, %rbx"
         emit "setl      %al"
         emit "movzbq    %al, %rax"
         emit "pushq     %rax"
@@ -141,7 +141,7 @@ module Targets
       def gti
         emit "popq      %rax"
         emit "popq      %rbx"
-        emit "cmpq      %rbx, %rax"
+        emit "cmpq      %rax, %rbx"
         emit "setg      %al"
         emit "movzbq    %al, %rax"
         emit "pushq     %rax"
@@ -151,7 +151,7 @@ module Targets
       def ltei
         emit "popq      %rax"
         emit "popq      %rbx"
-        emit "cmpq      %rbx, %rax"
+        emit "cmpq      %rax, %rbx"
         emit "setle     %al"
         emit "movzbq    %al, %rax"
         emit "pushq     %rax"
@@ -161,7 +161,7 @@ module Targets
       def gtei
         emit "popq      %rax"
         emit "popq      %rbx"
-        emit "cmpq      %rbx, %rax"
+        emit "cmpq      %rax, %rbx"
         emit "setge     %al"
         emit "movzbq    %al, %rax"
         emit "pushq     %rax"
index ba239dee46100a7a7ac24a2bc70bce0349a0bdd3..48e0867252fd0aabd6a1962e18aadd1f7eeb8ddd 100644 (file)
--- a/cerise.m
+++ b/cerise.m
@@ -1,9 +1,9 @@
 sum(a,b)
 {
-    return a<b
+    return a==b
 }
 
 main()
 {
-    return sum(1+1,2)
+    return sum(3,2)
 }
index e4e82a7a2a83c760cbd19e0ee7c97fb40a3f1cba..282b05a6826313f5625329dcbde890ca542f9f56 100755 (executable)
--- a/cerise.rb
+++ b/cerise.rb
@@ -37,7 +37,7 @@ class Lexer
   end
 
   SPACE = /([ \t\v\n\r]+|#.*\n)/
-  IDENT = /[_a-zA-Z][_a-zA-Z0-9]*!?/
+  IDENT = /[_a-zA-Z][_a-zA-Z0-9]*/
   BRACES = /[\(\)\[\]\{\}\.]/
   OPERATORS = /[:,<>*\/=+\-\$?!%]+/
   INTEGER = /[0-9]+/
@@ -76,10 +76,12 @@ class Lexer
       elsif @data.scan(OPERATORS)
         type = @data.matched
       end
-      Tok.new(@data.matched, @file, @data.pos, type) if type
+      tok = Tok.new(@data.matched, @file, @data.pos, type) if type
     else
-      Tok.new("EOF", @file, @data.pos, :eof)
+      tok = Tok.new("EOF", @file, @data.pos, :eof)
     end
+#    pp tok
+    tok
   end
 end
 
@@ -673,8 +675,10 @@ class Parser
   def expression()
     left = simple_expr()
     if matches_any(["==", "!=", "<", "<=", ">", ">="])
+      loc = location
+      op = consume()
       right = simple_expr()
-      left = IR::Op.new(op.location, nil, op.text, left, right)
+      left = IR::Op.new(loc, nil, op.text, left, right)
     end
     left
   end
@@ -682,18 +686,20 @@ class Parser
   def simple_expr()
     # Handle unary ops first
     if matches_any(["+", "-", :not])
+      loc = location
       op = consume()
       left = term()
-      left = IR::Op.new(op.location, nil, op.text, left, nil)
+      left = IR::Op.new(location, nil, op.text, left, nil)
     else
       left = term()
     end
 
     # now look for binary ops
     while matches_any(["+", "-", :or])
+      loc = location
       op = consume()
-      right = term();
-      left = IR::Op.new(location, nil, op.text, left, right)
+      right = term()
+      left = IR::Op.new(loc, nil, op.text, left, right)
     end
     left
   end
@@ -701,9 +707,10 @@ class Parser
   def term()
     left = factor()
     while matches_any(["*", "/", "%", :and])
+      loc = location
       op = consume()
       right = factor();
-      left = IR::Op.new(location, nil, op.text, left, right)
+      left = IR::Op.new(loc, nil, op.text, left, right)
     end
     left
   end
@@ -749,7 +756,7 @@ class Parser
     elsif tok.type == :ident
       IR::Var.new(tok.pos, nil, tok.text.to_sym)
     else
-      error("invalid constant")
+      error("invalid constant #{tok}")
     end
   end
 end
@@ -798,6 +805,16 @@ module Codegen
       puts "    modi"
     when "<"
       puts "    lti"
+    when "<="
+      puts "    ltei"
+    when ">"
+      puts "    gti"
+    when ">="
+      puts "    gtei"
+    when "=="
+      puts "    eqi"
+    when "!="
+      puts "    nei"
     else
       raise "not implemented"
     end