summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/functionfloat.cpp17
-rw-r--r--src/functionfloat.h16
-rw-r--r--src/functioninteger.cpp17
-rw-r--r--src/functioninteger.h16
-rw-r--r--src/functionrandom.cpp9
-rw-r--r--src/game.cpp4
-rw-r--r--src/gamebuilder.cpp2
-rw-r--r--src/parser.l8
-rw-r--r--src/parser.y6
-rw-r--r--src/variable.cpp34
10 files changed, 117 insertions, 12 deletions
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 @@
1#include "functionfloat.h"
2
3#include "gamestate.h"
4
5FunctionFloat::FunctionFloat()
6{
7}
8
9FunctionFloat::~FunctionFloat()
10{
11}
12
13void FunctionFloat::call( class GameState &gState )
14{
15 gState.push( gState.popDeref().to( Variable::tFloat ) );
16}
17
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 @@
1#ifndef FUNCTION_FLOAT_H
2#define FUNCTION_FLOAT_H
3
4#include "function.h"
5
6class FunctionFloat : public Function
7{
8public:
9 FunctionFloat();
10 virtual ~FunctionFloat();
11
12 virtual Bu::String getName() const { return "float"; }
13 virtual void call( class GameState &gState );
14};
15
16#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 @@
1#include "functioninteger.h"
2
3#include "gamestate.h"
4
5FunctionInteger::FunctionInteger()
6{
7}
8
9FunctionInteger::~FunctionInteger()
10{
11}
12
13void FunctionInteger::call( class GameState &gState )
14{
15 gState.push( gState.popDeref().to( Variable::tInt ) );
16}
17
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 @@
1#ifndef FUNCTION_INTEGER_H
2#define FUNCTION_INTEGER_H
3
4#include "function.h"
5
6class FunctionInteger : public Function
7{
8public:
9 FunctionInteger();
10 virtual ~FunctionInteger();
11
12 virtual Bu::String getName() const { return "integer"; }
13 virtual void call( class GameState &gState );
14};
15
16#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 )
20 if( vHigh.getType() != vLow.getType() ) 20 if( vHigh.getType() != vLow.getType() )
21 throw Bu::ExceptionBase("Different types in random!"); 21 throw Bu::ExceptionBase("Different types in random!");
22 22
23 double dRand = random()/(double)(RAND_MAX-1);
23 if( vLow.getType() == Variable::tInt ) 24 if( vLow.getType() == Variable::tInt )
24 { 25 {
25 gState.push( Variable( (int64_t)( 26 gState.push( Variable( (int64_t)(
26 (random()%(vHigh.getInt()-vLow.getInt()+1ll))+vLow.getInt() 27 (dRand*(vHigh.getInt()-vLow.getInt()+1ll))+vLow.getInt()
28 ) ) );
29 }
30 else if( vLow.getType() == Variable::tFloat )
31 {
32 gState.push( Variable( (double)(
33 (dRand*(vHigh.getFloat()-vLow.getFloat()))+vLow.getFloat()
27 ) ) ); 34 ) ) );
28 } 35 }
29} 36}
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 @@
5#include "functiondelete.h" 5#include "functiondelete.h"
6#include "functionexit.h" 6#include "functionexit.h"
7#include "functionrandom.h" 7#include "functionrandom.h"
8#include "functioninteger.h"
9#include "functionfloat.h"
8 10
9Game::Game() 11Game::Game()
10{ 12{
@@ -14,6 +16,8 @@ Game::Game()
14 addFunction( new FunctionDelete() ); 16 addFunction( new FunctionDelete() );
15 addFunction( new FunctionExit() ); 17 addFunction( new FunctionExit() );
16 addFunction( new FunctionRandom() ); 18 addFunction( new FunctionRandom() );
19 addFunction( new FunctionInteger() );
20 addFunction( new FunctionFloat() );
17} 21}
18 22
19Game::~Game() 23Game::~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 )
88 88
89void GameBuilder::closeSituationMode() 89void GameBuilder::closeSituationMode()
90{ 90{
91 //sio << "Set situation mode " << eCurSitMode << " to " << *pCurRoot << sio.nl; 91// sio << "Set situation <<" << pCurSit->getName() << ">> mode " << eCurSitMode << " to " << *pCurRoot << sio.nl;
92 pCurSit->setAst( pCurRoot, eCurSitMode ); 92 pCurSit->setAst( pCurRoot, eCurSitMode );
93 pCurRoot = pCurNode = NULL; 93 pCurRoot = pCurNode = NULL;
94} 94}
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; }
67 67
68[a-zA-Z_][a-zA-Z0-9_]* { yylval->sValue = new Bu::String( yytext ); return tokIdent; } 68[a-zA-Z_][a-zA-Z0-9_]* { yylval->sValue = new Bu::String( yytext ); return tokIdent; }
69 69
70[1-9][0-9]* { 70-?[1-9][0-9]* {
71 yylval->iValue = strtoll( yytext, NULL, 10 ); 71 yylval->iValue = strtoll( yytext, NULL, 10 );
72 return tokInt; 72 return tokInt;
73} 73}
@@ -76,10 +76,10 @@ null { return tokNull; }
76 return tokInt; 76 return tokInt;
77} 77}
78 78
79([1-9][0-9]*)?\.[0-9]* { 79-?([1-9][0-9]*|0)?\.[0-9]* {
80 printf("Parsing float: %s\n", yytext ); 80// printf("Parsing float: %s\n", yytext );
81 yylval->dValue = strtod( yytext, NULL ); 81 yylval->dValue = strtod( yytext, NULL );
82 printf("Final float: %f\n", yylval->dValue ); 82// printf("Final float: %f\n", yylval->dValue );
83 return tokFloat; 83 return tokFloat;
84} 84}
85 85
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
54%token tokDo 54%token tokDo
55%token tokIn 55%token tokIn
56%token tokIf 56%token tokIf
57%token tokThen 57%token tokThen "then"
58%token tokElse 58%token tokElse
59%token tokNot 59%token tokNot
60%token tokCommand 60%token tokCommand
@@ -204,7 +204,9 @@ ifnext:
204 | tokElse { bld.addNode( AstNode::tScope ); } '{' cmpltExprList '}' { 204 | tokElse { bld.addNode( AstNode::tScope ); } '{' cmpltExprList '}' {
205 bld.closeNode(); 205 bld.closeNode();
206 } 206 }
207 | tokElse { bld.addNode( AstNode::tScope ); } ifbase 207 | tokElse { bld.addNode( AstNode::tScope ); } ifbase {
208 bld.closeNode();
209 }
208 ; 210 ;
209 211
210varRef: tokIdent { bld.addVarRef( *($1), sidLocal ); } 212varRef: 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
463 { 463 {
464 return *this + rhs.to( eNew ); 464 return *this + rhs.to( eNew );
465 } 465 }
466 throw VariableException("Adding between dissimilar types is not yet supported.");
467 } 466 }
468 else 467 else
469 { 468 {
@@ -509,7 +508,16 @@ Variable Variable::operator-( const Variable &rhs ) const
509{ 508{
510 if( eType != rhs.eType ) 509 if( eType != rhs.eType )
511 { 510 {
512 throw VariableException("Subtracting between dissimilar types is not yet supported."); 511 Type eNew = bestType( eType, rhs.eType );
512
513 if( eType != eNew )
514 {
515 return to( eNew ) - rhs;
516 }
517 else
518 {
519 return *this - rhs.to( eNew );
520 }
513 } 521 }
514 else 522 else
515 { 523 {
@@ -551,7 +559,16 @@ Variable Variable::operator*( const Variable &rhs ) const
551{ 559{
552 if( eType != rhs.eType ) 560 if( eType != rhs.eType )
553 { 561 {
554 throw VariableException("Subtracting between dissimilar types is not yet supported."); 562 Type eNew = bestType( eType, rhs.eType );
563
564 if( eType != eNew )
565 {
566 return to( eNew ) * rhs;
567 }
568 else
569 {
570 return *this * rhs.to( eNew );
571 }
555 } 572 }
556 else 573 else
557 { 574 {
@@ -592,7 +609,16 @@ Variable Variable::operator/( const Variable &rhs ) const
592{ 609{
593 if( eType != rhs.eType ) 610 if( eType != rhs.eType )
594 { 611 {
595 throw VariableException("Subtracting between dissimilar types is not yet supported."); 612 Type eNew = bestType( eType, rhs.eType );
613
614 if( eType != eNew )
615 {
616 return to( eNew ) / rhs;
617 }
618 else
619 {
620 return *this / rhs.to( eNew );
621 }
596 } 622 }
597 else 623 else
598 { 624 {