TorqueScript  0.2
core.hpp
1 
15 #pragma once
16 
17 #include <memory>
18 #include <iostream>
19 #include <sstream>
20 #include <stdexcept>
21 
22 #include <torquescript/nativefunction.hpp>
23 #include <torquescript/executionscope.hpp>
24 #include <torquescript/interpreter.hpp>
25 #include <torquescript/executionstate.hpp>
26 #include <torquescript/storedvaluestack.hpp>
27 #include <torquescript/consoleobject.hpp>
28 #include <torquescript/fileobject.hpp>
29 
30 namespace TorqueScript
31 {
32  static void EchoBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state, const std::size_t argumentCount)
33  {
34  StoredValueStack& stack = state->mExecutionScope.getStack();
35 
36  std::string outputString = "";
37 
38  for (unsigned int iteration = 0; iteration < argumentCount; ++iteration)
39  {
40  // Parameters will flow right to left so we build the string considering this
41  std::string printedPayload = stack.popString(state);
42  outputString = printedPayload + outputString;
43  }
44 
45  state->mInterpreter->mConfig.mPlatform->logEcho(outputString);
46  stack.push_back(StoredValue(0));
47  }
48 
49  static void ExecBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state, const std::size_t argumentCount)
50  {
51  StoredValueStack& stack = state->mExecutionScope.getStack();
52 
53  for (unsigned int iteration = 0; iteration < argumentCount; ++iteration)
54  {
55  std::string executedFile = stack.popString(state);
56 
57  std::ostringstream output;
58  output << "Executing " << executedFile << " ...";
59  state->mInterpreter->mConfig.mPlatform->logEcho(output.str());
60 
61  state->mInterpreter->execute(executedFile, nullptr);
62  }
63 
64  stack.push_back(StoredValue(0));
65  }
66 
67  static void ActivatePackageBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state, const std::size_t argumentCount)
68  {
69  StoredValueStack& stack = state->mExecutionScope.getStack();
70 
71  for (unsigned int iteration = 0; iteration < argumentCount; ++iteration)
72  {
73  std::string activatedPackage = stack.popString(state);
74  state->mInterpreter->activateFunctionRegistry(activatedPackage);
75  }
76 
77  stack.push_back(StoredValue(0));
78  }
79 
80  static void DeactivatePackageBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state, const std::size_t argumentCount)
81  {
82  StoredValueStack& stack = state->mExecutionScope.getStack();
83 
84  for (unsigned int iteration = 0; iteration < argumentCount; ++iteration)
85  {
86  std::string deactivatedPackage = stack.popString(state);
87  state->mInterpreter->deactivateFunctionRegistry(deactivatedPackage);
88  }
89 
90  stack.push_back(StoredValue(0));
91  }
92 
93  static void DeleteBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state, const std::size_t argumentCount)
94  {
95  StoredValueStack& stack = state->mExecutionScope.getStack();
96 
97  state->mInterpreter->mConsoleObjectRegistry.removeConsoleObject(thisObject);
98  stack.push_back(StoredValue(0));
99  }
100 
101  static void GetNameBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state, const std::size_t argumentCount)
102  {
103  StoredValueStack& stack = state->mExecutionScope.getStack();
104 
105  const std::size_t stringID = state->mInterpreter->mStringTable.getOrAssign(state->mInterpreter->mConsoleObjectRegistry.getConsoleObjectName(thisObject));
106  stack.push_back(StoredValue(stringID, StoredValueType::String));
107  }
108 
109  static void GetClassNameBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state, const std::size_t argumentCount)
110  {
111  StoredValueStack& stack = state->mExecutionScope.getStack();
112 
113  const std::size_t stringID = state->mInterpreter->mStringTable.getOrAssign(thisObject->getClassName());
114  stack.push_back(StoredValue(stringID, StoredValueType::String));
115  }
116 
117  static void registerCoreLibrary(Interpreter* interpreter)
118  {
119  interpreter->addFunction(std::shared_ptr<Function>(new NativeFunction(EchoBuiltIn, PACKAGE_EMPTY, NAMESPACE_EMPTY, "echo")));
120  interpreter->addFunction(std::shared_ptr<Function>(new NativeFunction(ExecBuiltIn, PACKAGE_EMPTY, NAMESPACE_EMPTY, "exec")));
121  interpreter->addFunction(std::shared_ptr<Function>(new NativeFunction(ActivatePackageBuiltIn, PACKAGE_EMPTY, NAMESPACE_EMPTY, "activatePackage")));
122  interpreter->addFunction(std::shared_ptr<Function>(new NativeFunction(DeactivatePackageBuiltIn, PACKAGE_EMPTY, NAMESPACE_EMPTY, "deactivatePackage")));
123 
124  interpreter->addFunction(std::shared_ptr<Function>(new NativeFunction(GetClassNameBuiltIn, PACKAGE_EMPTY, "ConsoleObject", "getClassName")));
125  interpreter->addFunction(std::shared_ptr<Function>(new NativeFunction(GetNameBuiltIn, PACKAGE_EMPTY, "ConsoleObject", "getName")));
126  interpreter->addFunction(std::shared_ptr<Function>(new NativeFunction(DeleteBuiltIn, PACKAGE_EMPTY, "ConsoleObject", "delete")));
127  }
128 }
Definition: ast.hpp:28