From 6ebe2cff5fac1654e5b14f5d2ba03e7552bb2e8e Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Tue, 29 Jun 2010 10:36:42 +0200 Subject: [PATCH] extended expression parser for non-negative floats --- components/compiler/exprparser.cpp | 15 +++++++++++++-- components/compiler/generator.cpp | 9 ++++++++- components/compiler/generator.hpp | 2 ++ components/compiler/lineparser.hpp | 2 +- components/interpreter/docs/vmformat.txt | 3 ++- components/interpreter/genericopcodes.hpp | 14 +++++++++++++- components/interpreter/installopcodes.cpp | 1 + 7 files changed, 40 insertions(+), 6 deletions(-) diff --git a/components/compiler/exprparser.cpp b/components/compiler/exprparser.cpp index 117da6b75..1b5e36b6d 100644 --- a/components/compiler/exprparser.cpp +++ b/components/compiler/exprparser.cpp @@ -25,7 +25,13 @@ namespace Compiler bool ExprParser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner) { - return Parser::parseFloat (value, loc, scanner); + Operand operand; + operand.mType = 'f'; + operand.mFloat = value; + + mOperands.push_back (operand); + + return false; } bool ExprParser::parseName (const std::string& name, const TokenLoc& loc, @@ -57,7 +63,12 @@ namespace Compiler Operand operand = mOperands[mOperands.size()-1]; mOperands.clear(); - Generator::pushInt (code, mLiterals, operand.mInteger); + if (operand.mType=='l') + Generator::pushInt (code, mLiterals, operand.mInteger); + else if (operand.mType=='f') + Generator::pushFloat (code, mLiterals, operand.mFloat); + else + throw std::logic_error ("unknown expression type"); return operand.mType; } diff --git a/components/compiler/generator.cpp b/components/compiler/generator.cpp index 8f70a7863..8d37f57a9 100644 --- a/components/compiler/generator.cpp +++ b/components/compiler/generator.cpp @@ -67,7 +67,7 @@ namespace void opFloatToInt (Compiler::Generator::CodeContainer& code) { - assert (0); // not implemented + code.push_back (segment5 (6)); } void opStoreLocalShort (Compiler::Generator::CodeContainer& code) @@ -96,6 +96,13 @@ namespace Compiler opPushInt (code, index); opFetchIntLiteral (code); } + + void pushFloat (CodeContainer& code, Literals& literals, float value) + { + int index = literals.addFloat (value); + opPushInt (code, index); + opFetchFloatLiteral (code); + } void assignToLocal (CodeContainer& code, char localType, int localIndex, const CodeContainer& value, char valueType) diff --git a/components/compiler/generator.hpp b/components/compiler/generator.hpp index a055b3c71..061a30df4 100644 --- a/components/compiler/generator.hpp +++ b/components/compiler/generator.hpp @@ -15,6 +15,8 @@ namespace Compiler void pushInt (CodeContainer& code, Literals& literals, int value); + void pushFloat (CodeContainer& code, Literals& literals, float value); + void assignToLocal (CodeContainer& code, char localType, int localIndex, const CodeContainer& value, char valueType); } diff --git a/components/compiler/lineparser.hpp b/components/compiler/lineparser.hpp index 2f195902d..e9fae92b7 100644 --- a/components/compiler/lineparser.hpp +++ b/components/compiler/lineparser.hpp @@ -21,7 +21,7 @@ namespace Compiler { BeginState, ShortState, LongState, FloatState, - SetState, SetLocalVarState, SetLocalToState, + SetState, SetLocalVarState, EndState }; diff --git a/components/interpreter/docs/vmformat.txt b/components/interpreter/docs/vmformat.txt index 1449a6194..ca76d00f3 100644 --- a/components/interpreter/docs/vmformat.txt +++ b/components/interpreter/docs/vmformat.txt @@ -57,6 +57,7 @@ op 2: store stack[0] in local float stack[1] and pop twice op 3: convert stack[0] from integer to float op 4: replace stack[0] with integer literal index stack[0] op 5: replace stack[0] with float literal index stack[0] -opcodes 6-33554431 unused +op 6: convert stack[0] from float to integer +opcodes 7-33554431 unused opcodes 33554432-67108863 reserved for extensions diff --git a/components/interpreter/genericopcodes.hpp b/components/interpreter/genericopcodes.hpp index f5849676c..44e1e9cb4 100644 --- a/components/interpreter/genericopcodes.hpp +++ b/components/interpreter/genericopcodes.hpp @@ -22,11 +22,23 @@ namespace Interpreter virtual void execute (Runtime& runtime) { - Type_Data data = runtime[0]; + Type_Integer data = *reinterpret_cast (&runtime[0]); Type_Float floatValue = static_cast (data); runtime[0] = *reinterpret_cast (&floatValue); } }; + + class OpFloatToInt : public Opcode0 + { + public: + + virtual void execute (Runtime& runtime) + { + Type_Float data = *reinterpret_cast (&runtime[0]); + Type_Integer integerValue = static_cast (data); + runtime[0] = *reinterpret_cast (&integerValue); + } + }; } #endif diff --git a/components/interpreter/installopcodes.cpp b/components/interpreter/installopcodes.cpp index 44206696f..df3f69201 100644 --- a/components/interpreter/installopcodes.cpp +++ b/components/interpreter/installopcodes.cpp @@ -12,6 +12,7 @@ namespace Interpreter // generic interpreter.installSegment0 (0, new OpPushInt); interpreter.installSegment5 (3, new OpIntToFloat); + interpreter.installSegment5 (6, new OpFloatToInt); // local variables interpreter.installSegment5 (0, new OpStoreLocalShort);