]> git.mdlowis.com Git - proto/cerise-c.git/commitdiff
added runtime code for basic operators
authorMike Lowis <mike.lowis@gentex.com>
Mon, 3 Jun 2024 16:43:01 +0000 (12:43 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Mon, 3 Jun 2024 16:43:01 +0000 (12:43 -0400)
.gitignore
cerise-c.rb
runtime.h

index 96549872bcfbf193a309c98667b09208c9ef42b2..38b29858e734948dcce8ca3e027d2b435896cdb1 100644 (file)
@@ -1 +1,2 @@
-cerise-c cerise-c.c
+cerise-c
+cerise-c.c
index c6bae56fbfca6420c8cf321be6105f65fa52636c..f7f904ec81d66696a0a7906d82c0b3731b00b5e2 100755 (executable)
@@ -1085,9 +1085,27 @@ module Codegen
 
   def self.emit_binop(state, v)
     if v.op == "&&"
-      raise "logical and"
+      lvar = emit(state, v.left)
+      result = mktemp(state);
+      putln state, "Value #{result} = #{lvar};"
+      putln state, "if (IsTrue(#{result})) {"
+      state.indent += 1
+      rvar = emit(state, v.right)
+      putln state, "#{result} = #{rvar};"
+      state.indent -= 1
+      putln state, "} else {"
+      putln state, "    #{result} = MakeBool(false);"
+      putln state, "}"
     elsif v.op == "||"
-      raise "logical or"
+      lvar = emit(state, v.left)
+      result = mktemp(state);
+      putln state, "Value #{result} = #{lvar};"
+      putln state, "if (IsFalse(#{result})) {"
+      state.indent += 1
+      rvar = emit(state, v.right)
+      putln state, "#{result} = #{rvar};"
+      state.indent -= 1
+      putln state, "}"
     else
       lvar = emit(state, v.left)
       rvar = emit(state, v.right)
@@ -1110,18 +1128,11 @@ module Codegen
       when ">"
         putln state,  "Value #{result} = OpGt(#{lvar}, #{rvar});"
       when ">="
-        putln state,  "Value #{result} = OpGte(#{lvar}, #{rvar});"
+        putln state,  "Value #{result} = OpGtEq(#{lvar}, #{rvar});"
       when "=="
         putln state,  "Value #{result} = OpEq(#{lvar}, #{rvar});"
       when "!="
         putln state,  "Value #{result} = OpNeq(#{lvar}, #{rvar});"
-
-  #    when "&&"
-  #      putln state,  "Value #{result} = OpAnd(#{lvar}, #{rvar});"
-  #
-  #    when "||"
-  #      putln state,  "Value #{result} = OpOr(#{lvar}, #{rvar});"
-
       else
         raise "not implemented"
       end
index 20fb0481a62925eee0d83af1d56f7fcb2c388828..62aa67158aa77d0a373cf933a68a11b9f172f692 100644 (file)
--- a/runtime.h
+++ b/runtime.h
@@ -97,6 +97,11 @@ static inline bool IsString(Value val) {
 /* C Type Conversions
  *************************************************/
 
+static inline int32_t ValueAsBool(Value val) {
+    assert(IsBool(val));
+    return (bool)val.as_uint64;
+}
+
 static inline int32_t ValueAsInt(Value val) {
     assert(IsInt(val));
     return (int32_t)val.as_uint64;
@@ -216,3 +221,50 @@ static void Assert(char* file, int lineno, Value val) {
         exit(1);
     }
 }
+
+/* Binary Operators
+ *************************************************/
+
+static inline Value OpAdd(Value left, Value right) {
+    return MakeNil();
+}
+
+static inline Value OpSub(Value left, Value right) {
+    return MakeNil();
+}
+
+static inline Value OpMul(Value left, Value right) {
+    return MakeNil();
+}
+
+static inline Value OpDiv(Value left, Value right) {
+    return MakeNil();
+}
+
+static inline Value OpMod(Value left, Value right) {
+    return MakeNil();
+}
+
+static inline Value OpLt(Value left, Value right) {
+    return MakeNil();
+}
+
+static inline Value OpLtEq(Value left, Value right) {
+    return MakeNil();
+}
+
+static inline Value OpGt(Value left, Value right) {
+    return MakeNil();
+}
+
+static inline Value OpGtEq(Value left, Value right) {
+    return MakeNil();
+}
+
+static inline Value OpEq(Value left, Value right) {
+    return MakeNil();
+}
+
+static inline Value OpNeq(Value left, Value right) {
+    return MakeNil();
+}