Started working on c api/ fixed some bugs

This commit is contained in:
Ryan Ward 2020-12-08 23:48:42 -05:00
parent 7a996b76f8
commit b8b446143e
9 changed files with 89 additions and 203 deletions

View File

@ -5,6 +5,7 @@
#include <iostream> #include <iostream>
#include "value.h" #include "value.h"
#include "enviroment.h" #include "enviroment.h"
#include "sterilizer.h"
//#include <chrono> //#include <chrono>
using namespace dms; using namespace dms;
//typedef void(*FNPTR)(); //typedef void(*FNPTR)();

View File

@ -189,6 +189,7 @@
<ClInclude Include="errors.h" /> <ClInclude Include="errors.h" />
<ClInclude Include="dms.h" /> <ClInclude Include="dms.h" />
<ClInclude Include="LineParser.h" /> <ClInclude Include="LineParser.h" />
<ClInclude Include="sterilizer.h" />
<ClInclude Include="token.h" /> <ClInclude Include="token.h" />
<ClInclude Include="utils.h" /> <ClInclude Include="utils.h" />
<ClInclude Include="value.h" /> <ClInclude Include="value.h" />

View File

@ -25,6 +25,12 @@
<Filter Include="Source Files\DMS\blocks"> <Filter Include="Source Files\DMS\blocks">
<UniqueIdentifier>{4c0bb4c5-388c-4f15-abb7-143cb6d6232f}</UniqueIdentifier> <UniqueIdentifier>{4c0bb4c5-388c-4f15-abb7-143cb6d6232f}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Header Files\DMS\C-API">
<UniqueIdentifier>{f71964f6-66b3-4d8c-a8da-56d703ad1c2a}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\DMS\C-API">
<UniqueIdentifier>{32abbbfd-6359-488c-83be-f61da8fd8f4d}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="cmd.cpp"> <ClCompile Include="cmd.cpp">
@ -143,5 +149,8 @@
<ClInclude Include="comparisons.h"> <ClInclude Include="comparisons.h">
<Filter>Header Files\DMS</Filter> <Filter>Header Files\DMS</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="sterilizer.h">
<Filter>Header Files\DMS\C-API</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -3,6 +3,7 @@
//Implement the defaults //Implement the defaults
namespace dms { namespace dms {
// These are pointers to other data that should not be deleted. // These are pointers to other data that should not be deleted.
extern "C" { // Custom data needs to be c ready
const value* perNIL = new value; const value* perNIL = new value;
void dms_custom::_del() { void dms_custom::_del() {
state = nullptr; state = nullptr;
@ -23,11 +24,11 @@ namespace dms {
return false; return false;
} }
value dms_custom::Call(dms_args* args) { value dms_custom::Call(dms_args* args) {
state->push_error(errors::error{errors::unknown,"Attempting to call a non function value!"}); state->push_error(errors::error{ errors::unknown,"Attempting to call a non function value!" });
return nullptr; return nullptr;
} }
value dms_custom::ToString() { value dms_custom::ToString() {
return value(utils::concat("Custom: ",this)); return value(utils::concat("Custom: ", this));
} }
value dms_custom::ADD(value left, value right) { value dms_custom::ADD(value left, value right) {
state->push_error(errors::error{ errors::unknown,"Attempting to add a non number value!" }); state->push_error(errors::error{ errors::unknown,"Attempting to add a non number value!" });
@ -58,4 +59,5 @@ namespace dms {
value dms_custom::LESS_THAN_EQUAL(value left, value right) { value dms_custom::LESS_THAN_EQUAL(value left, value right) {
return value(left.getPrintable() <= right.getPrintable()); return value(left.getPrintable() <= right.getPrintable());
} }
}
} }

Binary file not shown.

21
DMS/sterilizer.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include "value.h"
// This file converts the c++ objects into a form that c is happy with
extern "C" {
using namespace dms;
// All structs types in this file will have an s_typename infront of it
// All methods will have an sf_methodname infron of it as well
struct s_value {
datatypes type = nil; // The type of the value
unsigned int slen; // Length of string
const char* s; // Contains the string part.
uint64_t i; // The int portion of the code
double n;
void setInt(int n); // Define value as an int
void setDouble(double n); // Define value as a double
void setString(const char* s); // Define value as a string
void setBlock(const char* s); // Define value as a block
void setVariable(const char* s); // Define value as a variable
void setNil(); // Define value as nil
};
}

View File

@ -356,123 +356,6 @@ namespace dms {
size_t dms_args::size() { size_t dms_args::size() {
return args.size(); return args.size();
} }
std::string dms_string::getValue(dms_state* state) {
std::vector<char> _temp;
std::vector<char> _var;
std::vector<char> _ind;
std::stringstream temp;
std::stringstream var;
std::stringstream ind;
bool varStart = false;
bool indStart = false;
for (size_t i = 0; i < val.size(); i++) {
if (indStart && val[i] == '`') {
std::string lookup = var.str();
std::string index = ind.str();
var.str("");
var.clear();
ind.str("");
ind.clear();
varStart = false;
indStart = false;
if (state->getMem()->count(lookup)) {
value v = (*state->getMem())[lookup];
if (v.type == datatypes::block) {
if (state->getCharacter(v.getPrintable()) != nullptr) {
character* cha = state->getCharacter(v.getPrintable());
if (cha->values.count(index)) {
temp << cha->values[index].getPrintable();
}
else {
temp << cha->getName();
}
}
else if (state->getEnvironment(v.getPrintable())!=nullptr) {
enviroment* env = state->getEnvironment(v.getPrintable());
if (env->values.count(index)) {
temp << env->values[index].getPrintable();
}
else {
temp << env;
}
}
else {
temp << "nil";
}
}
else if (v.resolve(state).type == datatypes::env) {
if(v.resolve(state).e->ipart.size()> std::stoi(index))
temp << v.resolve(state).e->ipart[std::stoi(index)-(int64_t)1].getPrintable();
else
temp << "nil";
}
else {
temp << v.resolve(state).getPrintable();
}
}
else {
temp << "nil";
}
}
else if (indStart && val[i] == ':') {
state->push_error(errors::error{ errors::badtoken,"Cannot index more than once in a string injection!" });
varStart = false;
indStart = false;
var.str("");
var.clear();
ind.str("");
ind.clear();
return "";
}
else if (indStart) {
ind << val[i];
}
else if (val[i] == '`' && !varStart) {
varStart = true;
}
else if (val[i] == '`' && varStart) {
std::string lookup = var.str();
var.str("");
var.clear();
varStart = false;
if (state->getMem()->count(lookup)) {
value v = (*state->getMem())[lookup];
if (v.type == datatypes::block) {
if (state->getCharacter(v.s)) {
temp << state->characters[v.s]->getName();
}
else {
temp << "nil";
}
}
else {
temp << v.resolve(state).getPrintable();
}
}
else {
temp << "nil";
}
}
// A space is not allowed at all
else if (val[i] == ':' && varStart) {
indStart = true;
}
else if (val[i] == ' ' && varStart) {
temp << var.str();
varStart = false;
var.str("");
var.clear();
}
else if (varStart) {
var << val[i];
}
else {
temp << val[i];
}
}
return temp.str();
}
std::string value::getPrintable() const { std::string value::getPrintable() const {
if (type == string) { if (type == string) {
return s; return s;
@ -608,9 +491,6 @@ namespace dms {
nuke(); nuke();
type = nil; type = nil;
} }
std::string dms_string::getValue() {
return val;
}
void dms_list::pushValue(value val) { void dms_list::pushValue(value val) {
ipart.push_back(val); ipart.push_back(val);
} }
@ -638,16 +518,4 @@ namespace dms {
} }
return str.str(); return str.str();
} }
std::ostream& operator << (std::ostream& out, const dms_string& c) {
out << c.val;
return out;
}
std::ostream& operator << (std::ostream& out, const dms_boolean& c) {
out << c.val;
return out;
}
std::ostream& operator << (std::ostream& out, const dms_number& c) {
out << c.val;
return out;
}
} }

View File

@ -12,23 +12,6 @@ namespace dms {
struct dms_state; struct dms_state;
extern const std::string datatype[]; extern const std::string datatype[];
enum datatypes { escape, nil, number, int64, boolean, env, string, custom, variable, block, error }; enum datatypes { escape, nil, number, int64, boolean, env, string, custom, variable, block, error };
struct dms_number {
double val;
double getValue() { return val; }
friend std::ostream& operator << (std::ostream& out, const dms_number& c);
};
struct dms_boolean {
bool val;
bool getValue() { return val; }
friend std::ostream& operator << (std::ostream& out, const dms_boolean& c);
};
struct dms_string {
std::string val;
std::string getValue();
std::string getValue(dms_state* state);
friend std::ostream& operator << (std::ostream& out, const dms_string& c);
};
// Custom data that you can work with by overriding this code // Custom data that you can work with by overriding this code
struct dms_custom { struct dms_custom {
void Init(dms_state* state); void Init(dms_state* state);

View File

@ -8,3 +8,4 @@ The Dialogue Management Script's goal is to provide a nice and simple way to hav
- [X] ~~Interpert all the bytecode~~ - [X] ~~Interpert all the bytecode~~
- [ ] Finish implementing custom datatype - [ ] Finish implementing custom datatype
- [ ] Speed up interperter - [ ] Speed up interperter
- [ ] Finish C API for code