From 8b9a15a755ebc6681ff6be808615e375cb567080 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Jan 2012 00:08:48 -0700 Subject: New functions, fixes, and a working bloodfields. --- src/functionfloat.cpp | 17 +++++++++++++++++ src/functionfloat.h | 16 ++++++++++++++++ src/functioninteger.cpp | 17 +++++++++++++++++ src/functioninteger.h | 16 ++++++++++++++++ src/functionrandom.cpp | 9 ++++++++- src/game.cpp | 4 ++++ src/gamebuilder.cpp | 2 +- src/parser.l | 8 ++++---- src/parser.y | 6 ++++-- src/variable.cpp | 34 ++++++++++++++++++++++++++++++---- 10 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 src/functionfloat.cpp create mode 100644 src/functionfloat.h create mode 100644 src/functioninteger.cpp create mode 100644 src/functioninteger.h (limited to 'src') diff --git a/src/functionfloat.cpp b/src/functionfloat.cpp new file mode 100644 index 0000000..7d6a0a5 --- /dev/null +++ b/src/functionfloat.cpp @@ -0,0 +1,17 @@ +#include "functionfloat.h" + +#include "gamestate.h" + +FunctionFloat::FunctionFloat() +{ +} + +FunctionFloat::~FunctionFloat() +{ +} + +void FunctionFloat::call( class GameState &gState ) +{ + gState.push( gState.popDeref().to( Variable::tFloat ) ); +} + diff --git a/src/functionfloat.h b/src/functionfloat.h new file mode 100644 index 0000000..ca72151 --- /dev/null +++ b/src/functionfloat.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_FLOAT_H +#define FUNCTION_FLOAT_H + +#include "function.h" + +class FunctionFloat : public Function +{ +public: + FunctionFloat(); + virtual ~FunctionFloat(); + + virtual Bu::String getName() const { return "float"; } + virtual void call( class GameState &gState ); +}; + +#endif diff --git a/src/functioninteger.cpp b/src/functioninteger.cpp new file mode 100644 index 0000000..049f9ca --- /dev/null +++ b/src/functioninteger.cpp @@ -0,0 +1,17 @@ +#include "functioninteger.h" + +#include "gamestate.h" + +FunctionInteger::FunctionInteger() +{ +} + +FunctionInteger::~FunctionInteger() +{ +} + +void FunctionInteger::call( class GameState &gState ) +{ + gState.push( gState.popDeref().to( Variable::tInt ) ); +} + diff --git a/src/functioninteger.h b/src/functioninteger.h new file mode 100644 index 0000000..2832c20 --- /dev/null +++ b/src/functioninteger.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_INTEGER_H +#define FUNCTION_INTEGER_H + +#include "function.h" + +class FunctionInteger : public Function +{ +public: + FunctionInteger(); + virtual ~FunctionInteger(); + + virtual Bu::String getName() const { return "integer"; } + virtual void call( class GameState &gState ); +}; + +#endif diff --git a/src/functionrandom.cpp b/src/functionrandom.cpp index 2665a14..ec302b3 100644 --- a/src/functionrandom.cpp +++ b/src/functionrandom.cpp @@ -20,10 +20,17 @@ void FunctionRandom::call( class GameState &gState ) if( vHigh.getType() != vLow.getType() ) throw Bu::ExceptionBase("Different types in random!"); + double dRand = random()/(double)(RAND_MAX-1); if( vLow.getType() == Variable::tInt ) { gState.push( Variable( (int64_t)( - (random()%(vHigh.getInt()-vLow.getInt()+1ll))+vLow.getInt() + (dRand*(vHigh.getInt()-vLow.getInt()+1ll))+vLow.getInt() + ) ) ); + } + else if( vLow.getType() == Variable::tFloat ) + { + gState.push( Variable( (double)( + (dRand*(vHigh.getFloat()-vLow.getFloat()))+vLow.getFloat() ) ) ); } } diff --git a/src/game.cpp b/src/game.cpp index c19b039..3a432d9 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -5,6 +5,8 @@ #include "functiondelete.h" #include "functionexit.h" #include "functionrandom.h" +#include "functioninteger.h" +#include "functionfloat.h" Game::Game() { @@ -14,6 +16,8 @@ Game::Game() addFunction( new FunctionDelete() ); addFunction( new FunctionExit() ); addFunction( new FunctionRandom() ); + addFunction( new FunctionInteger() ); + addFunction( new FunctionFloat() ); } Game::~Game() diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index d04a642..3cf2e1f 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp @@ -88,7 +88,7 @@ void GameBuilder::beginSituationMode( Situation::Mode m ) void GameBuilder::closeSituationMode() { - //sio << "Set situation mode " << eCurSitMode << " to " << *pCurRoot << sio.nl; +// sio << "Set situation <<" << pCurSit->getName() << ">> mode " << eCurSitMode << " to " << *pCurRoot << sio.nl; pCurSit->setAst( pCurRoot, eCurSitMode ); pCurRoot = pCurNode = NULL; } diff --git a/src/parser.l b/src/parser.l index 7b11765..e0bc340 100644 --- a/src/parser.l +++ b/src/parser.l @@ -67,7 +67,7 @@ null { return tokNull; } [a-zA-Z_][a-zA-Z0-9_]* { yylval->sValue = new Bu::String( yytext ); return tokIdent; } -[1-9][0-9]* { +-?[1-9][0-9]* { yylval->iValue = strtoll( yytext, NULL, 10 ); return tokInt; } @@ -76,10 +76,10 @@ null { return tokNull; } return tokInt; } -([1-9][0-9]*)?\.[0-9]* { - printf("Parsing float: %s\n", yytext ); +-?([1-9][0-9]*|0)?\.[0-9]* { +// printf("Parsing float: %s\n", yytext ); yylval->dValue = strtod( yytext, NULL ); - printf("Final float: %f\n", yylval->dValue ); +// printf("Final float: %f\n", yylval->dValue ); return tokFloat; } diff --git a/src/parser.y b/src/parser.y index 3dfd737..2e9eead 100644 --- a/src/parser.y +++ b/src/parser.y @@ -54,7 +54,7 @@ void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *err %token tokDo %token tokIn %token tokIf -%token tokThen +%token tokThen "then" %token tokElse %token tokNot %token tokCommand @@ -204,7 +204,9 @@ ifnext: | tokElse { bld.addNode( AstNode::tScope ); } '{' cmpltExprList '}' { bld.closeNode(); } - | tokElse { bld.addNode( AstNode::tScope ); } ifbase + | tokElse { bld.addNode( AstNode::tScope ); } ifbase { + bld.closeNode(); + } ; varRef: tokIdent { bld.addVarRef( *($1), sidLocal ); } diff --git a/src/variable.cpp b/src/variable.cpp index 965e1af..2ea8334 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -463,7 +463,6 @@ Variable Variable::operator+( const Variable &rhs ) const { return *this + rhs.to( eNew ); } - throw VariableException("Adding between dissimilar types is not yet supported."); } else { @@ -509,7 +508,16 @@ Variable Variable::operator-( const Variable &rhs ) const { if( eType != rhs.eType ) { - throw VariableException("Subtracting between dissimilar types is not yet supported."); + Type eNew = bestType( eType, rhs.eType ); + + if( eType != eNew ) + { + return to( eNew ) - rhs; + } + else + { + return *this - rhs.to( eNew ); + } } else { @@ -551,7 +559,16 @@ Variable Variable::operator*( const Variable &rhs ) const { if( eType != rhs.eType ) { - throw VariableException("Subtracting between dissimilar types is not yet supported."); + Type eNew = bestType( eType, rhs.eType ); + + if( eType != eNew ) + { + return to( eNew ) * rhs; + } + else + { + return *this * rhs.to( eNew ); + } } else { @@ -592,7 +609,16 @@ Variable Variable::operator/( const Variable &rhs ) const { if( eType != rhs.eType ) { - throw VariableException("Subtracting between dissimilar types is not yet supported."); + Type eNew = bestType( eType, rhs.eType ); + + if( eType != eNew ) + { + return to( eNew ) / rhs; + } + else + { + return *this / rhs.to( eNew ); + } } else { -- cgit v1.2.3