forked from teamnwah/openmw-tes3coop
extended expression parser for non-negative floats
This commit is contained in:
parent
bceb7ebcbd
commit
6ebe2cff5f
7 changed files with 40 additions and 6 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
@ -97,6 +97,13 @@ namespace Compiler
|
|||
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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ namespace Compiler
|
|||
{
|
||||
BeginState,
|
||||
ShortState, LongState, FloatState,
|
||||
SetState, SetLocalVarState, SetLocalToState,
|
||||
SetState, SetLocalVarState,
|
||||
EndState
|
||||
};
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -22,11 +22,23 @@ namespace Interpreter
|
|||
|
||||
virtual void execute (Runtime& runtime)
|
||||
{
|
||||
Type_Data data = runtime[0];
|
||||
Type_Integer data = *reinterpret_cast<Type_Integer *> (&runtime[0]);
|
||||
Type_Float floatValue = static_cast<Type_Float> (data);
|
||||
runtime[0] = *reinterpret_cast<Type_Data *> (&floatValue);
|
||||
}
|
||||
};
|
||||
|
||||
class OpFloatToInt : public Opcode0
|
||||
{
|
||||
public:
|
||||
|
||||
virtual void execute (Runtime& runtime)
|
||||
{
|
||||
Type_Float data = *reinterpret_cast<Type_Float *> (&runtime[0]);
|
||||
Type_Integer integerValue = static_cast<Type_Integer> (data);
|
||||
runtime[0] = *reinterpret_cast<Type_Data *> (&integerValue);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue