Fixed problem in DeviceSettings::strParam, returned wrong string
[avr-sim.git] / src / ScriptEngine.cpp
blobc576ccf2363bad83499ca08efab75c265ee05f33
1 /*
2 * avr-sim: An atmel AVR simulator
3 * Copyright (C) 2008 Tom Haber
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "ScriptEngine.h"
20 #include "script/ScriptException.h"
21 #include "Program.h"
23 #ifdef DEBUG
24 # include <iostream>
25 #endif
27 using script::ScriptMethod;
28 using script::ScriptUtil;
29 using script::VM;
31 namespace avr {
33 ScriptMethod<ScriptProgram> ScriptProgram::sm_methods[] = {
34 ScriptMethod<ScriptProgram>( "functionName", &ScriptProgram::script_functionName ),
35 ScriptMethod<ScriptProgram>( "dataName", &ScriptProgram::script_dataName ),
36 ScriptMethod<ScriptProgram>( "functionAddress", &ScriptProgram::script_functionAddress ),
37 ScriptMethod<ScriptProgram>( "dataAddress", &ScriptProgram::script_dataAddress )
40 ScriptProgram::ScriptProgram(script::VM *vm, const Program & program)
41 : Scriptable(vm), program(program), methodBase( -1 ) {
42 methodBase = ScriptUtil<ScriptProgram,Scriptable>::addMethods( this,
43 sm_methods, sizeof(sm_methods)/sizeof(sm_methods[0]) );
45 registerClass("Program");
48 ScriptProgram::~ScriptProgram() {
49 unregisterClass("Program");
52 int ScriptProgram::methodCall(int i) {
53 return ScriptUtil<ScriptProgram,Scriptable>::methodCall(this, i, methodBase,
54 sm_methods, sizeof(sm_methods)/sizeof(sm_methods[0]) );
57 int ScriptProgram::script_functionName(const char *funcName) {
58 if ( vm->top() != 1 || vm->getType(1) != VM::Number )
59 throw script::ScriptException("expects a function address");
61 unsigned int addr = (unsigned int)vm->toNumber(1);
62 vm->pushString( program.functionName(addr) );
63 return 1;
66 int ScriptProgram::script_dataName(const char *funcName) {
67 if ( vm->top() != 1 || vm->getType(1) != VM::Number )
68 throw script::ScriptException("expects a data address");
70 unsigned int addr = (unsigned int)vm->toNumber(1);
71 vm->pushString( program.dataName(addr) );
72 return 1;
75 int ScriptProgram::script_functionAddress(const char *funcName) {
76 if ( vm->top() != 1 || vm->getType(1) != VM::String )
77 throw script::ScriptException("expects a function name");
79 const char *name = vm->toString(1);
80 int addr = program.functionAddress(name);
81 if( addr == -1 )
82 throw script::ScriptException("Unable to find function address");
84 vm->pushNumber( addr );
85 return 1;
88 int ScriptProgram::script_dataAddress(const char *funcName) {
89 if ( vm->top() != 1 || vm->getType(1) != VM::String )
90 throw script::ScriptException("expects a data name");
92 const char *name = vm->toString(1);
93 int addr = program.dataAddress(name);
94 if( addr == -1 )
95 throw script::ScriptException("Unable to find data address");
97 vm->pushNumber( addr );
98 return 1;
101 ScriptEngine::ScriptEngine() : program(0) {
102 #ifdef DEBUG
103 std::cout << "Booting virtual machine" << std::endl;
104 #endif
107 ScriptEngine::~ScriptEngine() {
108 #ifdef DEBUG
109 std::cout << "Unloading virtual machine" << std::endl;
110 #endif
112 if( program != 0 )
113 delete program;
116 void ScriptEngine::setProgram(const Program & prog) {
117 program = new ScriptProgram( this, prog );
120 void ScriptEngine::loadConfig(const char *filename) {
121 try {
122 compileFile(filename);
123 } catch( script::ScriptException & ex ) {
124 throw ScriptException( ex.message() );