From 3133b5ab85bd7fa9ae08f1f544ba264c9f1f1620 Mon Sep 17 00:00:00 2001 From: Mike Lowis Date: Mon, 13 Nov 2023 16:19:39 -0500 Subject: [PATCH] fixed up binary ops --- aas.rb | 4 ++-- cerise.m | 2 +- cerise.rb | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/aas.rb b/aas.rb index 672cff8..0d06a05 100755 --- 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 diff --git a/cerise.m b/cerise.m index fc7e1bb..9c3468f 100644 --- a/cerise.m +++ b/cerise.m @@ -1,4 +1,4 @@ main() { - return 43 % 2 + return 42 * 2 } diff --git a/cerise.rb b/cerise.rb index 6daa8e5..513eb6b 100755 --- 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 "/" -- 2.54.0