From: Mike Lowis Date: Mon, 3 Jun 2024 16:43:01 +0000 (-0400) Subject: added runtime code for basic operators X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=23b0658d612c4242d074db9abb9ddea317823f81;p=proto%2Fcerise-c.git added runtime code for basic operators --- diff --git a/.gitignore b/.gitignore index 9654987..38b2985 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -cerise-c cerise-c.c +cerise-c +cerise-c.c diff --git a/cerise-c.rb b/cerise-c.rb index c6bae56..f7f904e 100755 --- a/cerise-c.rb +++ b/cerise-c.rb @@ -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 diff --git a/runtime.h b/runtime.h index 20fb048..62aa671 100644 --- 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(); +}