def TestIfElseStatement(): tp = MockTokenParser('if ( 1 ) var = 11 ; else var = 12 ;') ce = CompileEngine(tp, MockVariables(['var', 'mock']), None) script = MockScript('name') try: ce.CompileSingleExecutionBlock(script) except CompileError as e: print('TestIfElseStatement got a compile error:', e.value) script.CompareScript( 'cmpifest', [ IC.pushintvalue, 1, IC.jne, 24, #\ IC.pushintvalue, # | 11, # | IC.popglobalvariable, # | 'var', # | IC.jall, # | 16, # | #\ IC.pushintvalue, #/ # | 12, # | IC.popglobalvariable, # | 'var' ]) # |
def TestExpressionParserVariables(): ep = ExpressionParser(MockVariables(['var1', 'var2'])) script = MockScript('name') ep.ParseExpression(MockTokenParser('var1 / var2'), script) script.CompareScript('texprv', [ IC.pushglobalvariable, 'var1', IC.pushglobalvariable, 'var2', IC.devide ])
def TestIfStatement(): tp = MockTokenParser('{ if ( 1 ) var1 = 11 ; var2 = 12 ; }') ce = CompileEngine(tp, MockVariables([ 'var1', 'var2', ]), None) script = MockScript('name') try: ce.CompileSingleExecutionBlock(script) except CompileError as e: print('TestIfStatement got a compile error:', e.value) script.CompareScript( 'cmpifst', [ IC.pushintvalue, 1, IC.jne, 17, #\ IC.pushintvalue, # | 11, # | IC.popglobalvariable, # | 'var1', # | IC.pushintvalue, #/ 12, IC.popglobalvariable, 'var2' ])
def TestUnreferencedGlobalVariable(): ep = ExpressionParser(MockVariables(['var1', 'var2'])) script = MockScript('name') ep.ParseExpression(MockTokenParser('var1 / var2 + var3'), script) script.CompareScript('texprv', [ IC.pushglobalvariable, 'var1', IC.pushglobalvariable, 'var2', IC.devide, IC.pushglobalvariable, 'var3', IC.add ])
def TestExpressionParserNumeric(): ep = ExpressionParser(MockVariables()) script = MockScript('name') ep.ParseExpression(MockTokenParser('2 + 3 * 4 - 5'), script) script.CompareScript('texprn', [ IC.pushintvalue, 2, IC.pushintvalue, 3, IC.pushintvalue, 4, IC.multiply, IC.add, IC.pushintvalue, 5, IC.subtract ])
def TestLocalVariablesCanBeDeclared(): tp = MockTokenParser('local newlocal ; newlocal = 4 ;') ce = CompileEngine(tp, MockVariables(['global'], ['local']), None) script = MockScript('name') ce.CompileSingleExecutionBlock(script) ce.CompileSingleExecutionBlock(script) script.CompareScript('testscrlocvar', [IC.pushintvalue, 4, IC.poplocalvariable, 'newlocal'])
def TestGlobalVariablesCanBeAssigned(): tp = MockTokenParser('globalvar = globalvar ;') ce = CompileEngine(tp, MockVariables(['globalvar'], ['localvar']), None) script = MockScript('name') ce.CompileSingleExecutionBlock(script) script.CompareScript('testglva', [ IC.pushglobalvariable, 'globalvar', IC.popglobalvariable, 'globalvar' ])
def TestExpressionParserComplex(): variables = MockVariables(['var1']) ep = ExpressionParser(variables) script = MockScript('name') ep.ParseExpression(MockTokenParser('- 2 * ( var1 == 4 )'), script) script.CompareScript('texprcx', [ IC.pushintvalue, 2, IC.negate, IC.pushglobalvariable, 'var1', IC.pushintvalue, 4, IC.equals, IC.multiply ])
def TestHashCalculatorUsesSignedCRC32(): ep = ExpressionParser(MockVariables()) script = MockScript('name') ep.ParseExpression(MockTokenParser('[ hello-world ]'), script) script.CompareScript( 'hashs', [ IC.pushintvalue, #-1311505829 #2983461467 2983461467 ])
def TestExpressionParserString(): ep = ExpressionParser(MockVariables()) script = MockScript('name') ep.ParseExpression(MockTokenParser('"Hello" + "World"'), script) script.CompareScript('stringexp', [ IC.pushstring, 'Hello', IC.pushstring, 'World', IC.add, ])
def TestFnRoutineDropSkipJump(): tp = MockTokenParser('engineFunction ( ) ;') ce = CompileEngine(tp, MockVariables([], [], [['engineFunction', 0]]), None) script = MockScript('name') try: ce.CompileSingleExecutionBlock(script) except CompileError as e: print('TestFnRoutineDropSkipJump got a compile error:', e.value) script.CompareScript( 'fndsk', [IC.callfnroutine, 0, 'engineFunction', IC.dropskippausenonzero, -27])
def TestEngineFunctionInvalid(): tp = MockTokenParser('engineFunction ;') try: script = MockScript('name') ce = CompileEngine(tp, MockVariables([], [], [['engineFunction', 0]]), None) ce.CompileSingleExecutionBlock(script) AssertTrue(False) except CompileError as e: pass
def TestHashCalculator(): ep = ExpressionParser(MockVariables()) script = MockScript('name') ep.ParseExpression(MockTokenParser('[ hashtext ]'), script) script.CompareScript('hash', [IC.pushintvalue, 1976965134])
def TestFnRoutineNoParameters(): variables = MockVariables(['var1'], [], [['engineFunction', 0]]) ep = ExpressionParser(variables) script = MockScript('name') ep.ParseExpression(MockTokenParser('engineFunction ( )'), script) script.CompareScript('fnnop', [IC.callfnroutine, 0, 'engineFunction'])
def TestPauseStatement(): tp = MockTokenParser('pause ;') script = MockScript('name') ce = CompileEngine(tp, VariableEngine(), None) ce.CompileSingleExecutionBlock(script) script.CompareScript('pause', [IC.pause])
def CreateScript(self, name): self.scripts[name] = MockScript(name) return self.scripts[name]