(slightly) working version. Includes to other files needs fixing, then it should work

This commit is contained in:
sam-astro 2022-01-06 16:51:04 -05:00
parent b99cb9ba82
commit 41b90c3b4b
13 changed files with 113 additions and 71 deletions

11
Slang/CMakeLists.txt Normal file
View File

@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.10)
# set the project name
project(Slang)
# add the executable
#add_executable(Main.cpp)
target_include_directories(Slang PUBLIC
"${PROJECT_BINARY_DIR}"
)

View File

@ -7,7 +7,7 @@
#include <limits>
#include <algorithm>
#include <cstdlib>
#include <any>
#include <boost/any.hpp>
#include <unordered_map>
#include "eval.h"
@ -19,10 +19,10 @@
using namespace std;
using namespace boost;
unordered_map<string, any> globalVariableValues;
unordered_map<string, boost::any> globalVariableValues;
unordered_map<string, vector<vector<string>>> functionValues;
any GetVariableValue(const string& varName, const unordered_map<string, any>& variableVals)
boost::any GetVariableValue(const string& varName, const unordered_map<string, boost::any>& variableVals)
{
auto iA = variableVals.find(varName);
if (iA != variableVals.end())
@ -43,7 +43,7 @@ any GetVariableValue(const string& varName, const unordered_map<string, any>& va
}
}
bool IsVar(const string& varName, const unordered_map<string, any>& variableVals)
bool IsVar(const string& varName, const unordered_map<string, boost::any>& variableVals)
{
if (variableVals.find(varName) != variableVals.end())
return true;
@ -51,9 +51,9 @@ bool IsVar(const string& varName, const unordered_map<string, any>& variableVals
return false;
}
vector<any> VarValues(const vector<string>& varNames, const unordered_map<string, any>& variableVals)
vector<boost::any> VarValues(const vector<string>& varNames, const unordered_map<string, boost::any>& variableVals)
{
vector<any> realValues;
vector<boost::any> realValues;
for (int varIndex = 0; varIndex < varNames.size(); varIndex++)
{
@ -85,7 +85,7 @@ bool IsFunction(const string& funcName)
return false;
}
any EvalExpression(const string& ex, const unordered_map<string, any>& variableVals)
boost::any EvalExpression(const string& ex, const unordered_map<string, boost::any>& variableVals)
{
string expression = trim(ex);
bool inQuotes = false;
@ -221,10 +221,10 @@ any EvalExpression(const string& ex, const unordered_map<string, any>& variableV
return evaluate(newExpression);
}
bool BooleanLogic(const string& valA, const string& determinant, const string& valB, const unordered_map<string, any>& variableVals)
bool BooleanLogic(const string& valA, const string& determinant, const string& valB, const unordered_map<string, boost::any>& variableVals)
{
any valARealValue = EvalExpression(valA, variableVals);
any valBRealValue = EvalExpression(valB, variableVals);
boost::any valARealValue = EvalExpression(valA, variableVals);
boost::any valBRealValue = EvalExpression(valB, variableVals);
if (determinant == "==")
return AnyAsString(valARealValue) == AnyAsString(valBRealValue);
@ -244,7 +244,7 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v
return false;
}
int varOperation(const vector<string>& str, unordered_map<string, any>& variableValues)
int varOperation(const vector<string>& str, unordered_map<string, boost::any>& variableValues)
{
if (IsVar(str[0], variableValues))
{
@ -284,16 +284,16 @@ int varOperation(const vector<string>& str, unordered_map<string, any>& variable
return 1;
}
any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<string, any>& variableValues)
boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<string, boost::any>& variableValues)
{
if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/')
return any{};
return boost::any{};
// If print statement (deprecated, now use CPP.System.Print() function)
else if (words[lineNum][0] == "print")
{
cout << AnyAsString(EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl;
return any{};
return boost::any{};
}
// Check if function return
@ -308,7 +308,7 @@ any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<
else if (IsFunction(trim(split(words[lineNum][0], '(')[0])))
{
ExecuteFunction(trim(split(words[lineNum][0], '(')[0]), VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), trim(split(words[lineNum][0], '(')[0]), "")), ','), variableValues));
return any{};
return boost::any{};
}
// Iterate through all types to see if line inits or
@ -316,7 +316,7 @@ any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<
else if (countInVector(types, words[lineNum][0]) > 0)
{
variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 3, words[lineNum].end())), variableValues);
return any{};
return boost::any{};
}
// Check existing variables: If matches, then it means
@ -325,7 +325,7 @@ any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<
{
// Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right
varOperation(vector<string>(words[lineNum].begin(), words[lineNum].end()), variableValues);
return any{};
return boost::any{};
}
// Gathers while loop contents
@ -360,12 +360,12 @@ any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<
//Iterate through all lines in while loop
for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++)
{
any returnVal = ProcessLine(innerWords, lineNum, variableValues);
boost::any returnVal = ProcessLine(innerWords, lineNum, variableValues);
if (AnyAsString(returnVal) != "")
return returnVal;
}
}
return any{};
return boost::any{};
}
// Gathers if statement contents
@ -402,7 +402,7 @@ any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<
//Iterate through all lines in if statement
for (int l = 0; l < (int)ifContents.size(); l++)
{
any returnVal = ProcessLine(innerWords, l, variableValues);
boost::any returnVal = ProcessLine(innerWords, l, variableValues);
if (!returnVal.empty())
return returnVal;
}
@ -438,9 +438,9 @@ any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<
{
ProcessLine(innerWords, lineNum, variableValues);
}
return any{};
return boost::any{};
}
return any{};
return boost::any{};
}
//// Gathers else statement contents
//if (words[lineNum][0] == "else")
@ -448,15 +448,15 @@ any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<
//
//}
return any{};
return boost::any{};
}
any ExecuteFunction(const string functionName, const vector<any> inputVarVals)
boost::any ExecuteFunction(const string& functionName, const vector<boost::any>& inputVarVals)
{
// Get contents of function
vector<vector<string>> words = functionValues[functionName];
unordered_map<string, any> variableValues;
unordered_map<string, boost::any> variableValues;
vector<string> args = split(functionValues[functionName][0][0], ',');
for (int i = 0; i < (int)inputVarVals.size(); i++)
{
@ -466,7 +466,7 @@ any ExecuteFunction(const string functionName, const vector<any> inputVarVals)
//Iterate through all lines in function
for (int lineNum = 0; lineNum < (int)words.size(); lineNum++)
{
any returnVal = 0;
boost::any returnVal = 0;
try
{
returnVal = ProcessLine(words, lineNum, variableValues);
@ -478,7 +478,7 @@ any ExecuteFunction(const string functionName, const vector<any> inputVarVals)
if (!returnVal.empty())
return returnVal;
}
return any{};
return boost::any{};
}
int parseSlang(string script)
@ -543,7 +543,7 @@ int parseSlang(string script)
}
// Executes main, which is the starting function
ExecuteFunction("Main", vector<any> {"hi", 0});
ExecuteFunction("Main", vector<boost::any> {"hi", 0});
return 0;
}

View File

@ -65,9 +65,11 @@
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="boost_x64_release.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="boost_x64_release.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@ -89,12 +91,12 @@
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<DebugInformationFormat>OldStyle</DebugInformationFormat>
<AdditionalIncludeDirectories>D:\Code\boost</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>D:\Code\boost\libs;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>D:\Code\boost</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>D:\Code\boost\stage\lib</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -158,6 +160,9 @@
<None Include="builtin.slg" />
<None Include="script.slg" />
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@ -49,4 +49,7 @@
<None Include="script.slg" />
<None Include="builtin.slg" />
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />
</ItemGroup>
</Project>

View File

@ -3,37 +3,39 @@
#define ANYOPS_H
#include "builtin.h"
#include <any>
#include <boost/any.hpp>
using namespace boost;
using namespace std;
int LogWarning(const string& warningText);
// Will convert type 'any' val to a bool
bool AnyAsBool(const any& val)
bool AnyAsBool(const boost::any& val)
{
try // Try converting to bool
{
return any_cast<bool>(val);
}
catch (boost::bad_any_cast &e)
catch (boost::bad_any_cast)
{
try // Try converting to string
{
return any_cast<string>(val) == "true";
}
catch (boost::bad_any_cast &e)
catch (boost::bad_any_cast)
{
try // Try converting to float
{
return any_cast<float>(val) == 1.0f;
}
catch (boost::bad_any_cast &e) // Try converting to int
catch (boost::bad_any_cast) // Try converting to int
{
try
{
return any_cast<int>(val) == 1;
}
catch (boost::bad_any_cast &e) // Does not convert, return
catch (boost::bad_any_cast) // Does not convert, return
{
LogWarning("invalid conversion to type \'bool\'");
return false;
@ -44,25 +46,25 @@ bool AnyAsBool(const any& val)
}
// Will convert type 'any' val to a string
string AnyAsString(const any& val)
string AnyAsString(const boost::any& val)
{
try // Try converting to string
{
return any_cast<string>(val);
}
catch (boost::bad_any_cast &e)
catch (boost::bad_any_cast)
{
try // Try converting to int
{
return to_string(any_cast<int>(val));
}
catch (boost::bad_any_cast &e)
catch (boost::bad_any_cast)
{
try // Try converting to float
{
return to_string(any_cast<float>(val));
}
catch (boost::bad_any_cast &e) // Try converting to bool
catch (boost::bad_any_cast) // Try converting to bool
{
try
{
@ -70,7 +72,7 @@ string AnyAsString(const any& val)
if (any_cast<bool>(val) == true) i = "true";
return i;
}
catch (boost::bad_any_cast &e) // Does not convert, return
catch (boost::bad_any_cast) // Does not convert, return
{
LogWarning("invalid conversion to type \'string\'");
return "";
@ -81,25 +83,25 @@ string AnyAsString(const any& val)
}
// Will convert type 'any' val to a float
float AnyAsFloat(const any& val)
float AnyAsFloat(const boost::any& val)
{
try // Try converting to float
{
return any_cast<float>(val);
}
catch (boost::bad_any_cast &e)
catch (boost::bad_any_cast)
{
try // Try converting to int
{
return (float)any_cast<int>(val);
}
catch (boost::bad_any_cast &e)
catch (boost::bad_any_cast)
{
try // Try converting to string, then converting it to float
{
return stof(any_cast<string>(val));
}
catch (boost::bad_any_cast &e) // Try converting to bool
catch (boost::bad_any_cast) // Try converting to bool
{
try
{
@ -107,7 +109,7 @@ float AnyAsFloat(const any& val)
if (any_cast<bool>(val) == true) i = 1;
return i;
}
catch (boost::bad_any_cast &e) // Does not convert, return
catch (boost::bad_any_cast) // Does not convert, return
{
LogWarning("invalid conversion to type \'float\'");
return 0;
@ -118,25 +120,25 @@ float AnyAsFloat(const any& val)
}
// Will convert type 'any' val to an integer
int AnyAsInt(const any& val)
int AnyAsInt(const boost::any& val)
{
try // Try converting to int
{
return any_cast<int>(val);
}
catch (boost::bad_any_cast &e)
catch (boost::bad_any_cast)
{
try // Try converting to float
{
return (int)any_cast<float>(val);
}
catch (boost::bad_any_cast &e)
catch (boost::bad_any_cast)
{
try // Try converting to string, then converting it to int
{
return stoi(any_cast<string>(val));
}
catch (boost::bad_any_cast &e) // Try converting to bool
catch (boost::bad_any_cast) // Try converting to bool
{
try
{
@ -144,7 +146,7 @@ int AnyAsInt(const any& val)
if (any_cast<bool>(val) == true) i = 1;
return i;
}
catch (boost::bad_any_cast &e) // Does not convert, return
catch (boost::bad_any_cast) // Does not convert, return
{
LogWarning("invalid conversion to type \'int\'");
return 0;
@ -156,35 +158,35 @@ int AnyAsInt(const any& val)
// Gets type of 'any' val
// 0 -> int; 1 -> float; 2 -> bool; 3 -> string;
int any_type(const any& val)
int any_type(const boost::any& val)
{
try // Try converting to int
{
int i = any_cast<int>(val);
return 0;
}
catch (boost::bad_any_cast &e)
catch (boost::bad_any_cast)
{
try // Try converting to float
{
float f = any_cast<float>(val);
return 1;
}
catch (boost::bad_any_cast &e)
catch (boost::bad_any_cast)
{
try // Try converting to bool
{
bool b = any_cast<bool>(val);
return 2;
}
catch (boost::bad_any_cast &e) // Try converting to string
catch (boost::bad_any_cast) // Try converting to string
{
try
{
string s = any_cast<string>(val);
return 3;
}
catch (boost::bad_any_cast &e) // Does not convert, return
catch (boost::bad_any_cast) // Does not convert, return
{
LogWarning("variable has no type");
return -1;

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_PropertySheetDisplayName>boost_x64_release</_PropertySheetDisplayName>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>D:\Code\boost;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>D:\Code\boost\stage\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>

View File

@ -7,11 +7,11 @@
#include <regex>
#include <limits>
#include <algorithm>
#include <any>
#include <unordered_map>
#include "strops.h"
#include "graphics.h"
#include "anyops.h"
#include <boost/any.hpp>
using namespace std;
using namespace boost;
@ -19,7 +19,7 @@ using namespace boost;
vector<string> types = { "int", "float", "string", "bool", "void", "null" };
unordered_map<string, vector<vector<string>>> builtinFunctionValues;
unordered_map<string, any> builtinVarVals;
unordered_map<string, boost::any> builtinVarVals;
Parser mainWindow;
@ -105,7 +105,7 @@ int GetBuiltins(const string& s)
}
// Executes
any CPPFunction(const string& name, const vector<any>& args)
boost::any CPPFunction(const string& name, const vector<boost::any>& args)
{
if (name == "CPP.Math.Sin")
return sin(AnyAsFloat(args[0]));

View File

@ -7,7 +7,7 @@
#include <stack>
#include "eval.h"
#include "strops.h"
#include "builtin.h"
// #include "builtin.h"
using namespace std;
float precedence(const char& op) {
@ -29,7 +29,7 @@ float applyOp(const float& a, const float& b, const char& op) {
case '^': return pow(a, b);
}
string s(1, op);
LogWarning("operator \'" + s + "\' does not exist");
//LogWarning("operator \'" + s + "\' does not exist");
return 0;
}

View File

@ -1,6 +1,6 @@
#ifndef EVAL_H
#define EVAL_H
float evaluate(std::string tokens);
float evaluate(const std::string& t);
#endif

View File

@ -11,6 +11,7 @@
#include <regex>
#include <limits>
#include <algorithm>
#include <boost/any.hpp>
#include "strops.h"
#include "builtin.h"
#include "main.h"
@ -34,7 +35,7 @@ public:
bool OnUserUpdate(float fElapsedTime) override
{
ExecuteFunction("Update", vector<any> {});
ExecuteFunction("Update", vector<boost::any> {});
// Called once per frame
//for (int x = 0; x < ScreenWidth(); x++)

View File

@ -3,8 +3,7 @@
#define MAIN_H
using namespace std;
using namespace boost;
any ExecuteFunction(const string functionName, const vector<any> inputVarVals);
boost::any ExecuteFunction(const string& functionName, const vector<boost::any>& inputVarVals);
#endif

View File

@ -3,8 +3,9 @@
#include <string>
#include <regex>
#include <limits>
#include <boost/any.hpp>
#include "strops.h"
#include "builtin.h"
//#include "builtin.h"
using namespace std;
const string WHITESPACE = " \n\r\t\f\v";
@ -20,11 +21,12 @@ bool isNumber(const string& str)
bool stob(string str)
{
transform(str.begin(), str.end(), str.begin(), ::tolower);
istringstream is(str);
bool b;
is >> boolalpha >> b;
return b;
//transform(str.begin(), str.end(), str.begin(), ::tolower);
//istringstream is(str);
//bool b;
//is >> boolalpha >> b;
//return b;
return false;
}
string StringRaw(const string& s)

View File

@ -1,6 +1,8 @@
#ifndef STROPS_H
#define STROPS_H
#include <boost/any.hpp>
using namespace std;
bool isNumber(const string& str);