wip
This commit is contained in:
parent
186cab9ac9
commit
30457cd81a
@ -51,7 +51,7 @@ Value Context::eval(peg::Ast& ast) {
|
|||||||
for (int i=0; i<ast.nodes.size(); i++) {
|
for (int i=0; i<ast.nodes.size(); i++) {
|
||||||
exprs.args.push_back(getValue(eval(*nodes[i])));
|
exprs.args.push_back(getValue(eval(*nodes[i])));
|
||||||
}
|
}
|
||||||
return Value(exprs);
|
return exprs;
|
||||||
} else if (ast.name == "AdditionExpr") {
|
} else if (ast.name == "AdditionExpr") {
|
||||||
Value value = getValue(eval(*nodes[0]));
|
Value value = getValue(eval(*nodes[0]));
|
||||||
for (int i=1; i<nodes.size(); i+=2) {
|
for (int i=1; i<nodes.size(); i+=2) {
|
||||||
@ -59,9 +59,9 @@ Value Context::eval(peg::Ast& ast) {
|
|||||||
Value v2 = getValue(eval(*nodes[i+1]));
|
Value v2 = getValue(eval(*nodes[i+1]));
|
||||||
// TODO: floats
|
// TODO: floats
|
||||||
if (op == 0) {
|
if (op == 0) {
|
||||||
value = Value(value.getInt(cxt) + v2.getInt(cxt));
|
value = value.getInt(cxt) + v2.getInt(cxt);
|
||||||
} else {
|
} else {
|
||||||
value = Value(value.getInt(cxt) - v2.getInt(cxt));
|
value = value.getInt(cxt) - v2.getInt(cxt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
@ -72,9 +72,9 @@ Value Context::eval(peg::Ast& ast) {
|
|||||||
Value v2 = getValue(eval(*nodes[i+1]));
|
Value v2 = getValue(eval(*nodes[i+1]));
|
||||||
// TODO: floats
|
// TODO: floats
|
||||||
if (op == 0) {
|
if (op == 0) {
|
||||||
value = Value(value.getInt(cxt) * v2.getInt(cxt));
|
value = value.getInt(cxt) * v2.getInt(cxt);
|
||||||
} else {
|
} else {
|
||||||
value = Value(value.getInt(cxt) / v2.getInt(cxt));
|
value = value.getInt(cxt) / v2.getInt(cxt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
@ -84,22 +84,22 @@ Value Context::eval(peg::Ast& ast) {
|
|||||||
auto v1 = currentScope->getVar(*this, name);
|
auto v1 = currentScope->getVar(*this, name);
|
||||||
int op = (*nodes[1]).choice;
|
int op = (*nodes[1]).choice;
|
||||||
auto v2 = getValue(eval(*nodes[2]));
|
auto v2 = getValue(eval(*nodes[2]));
|
||||||
Value result = Value(Nil());
|
Value result = Nil();
|
||||||
// TODO: floats
|
// TODO: floats
|
||||||
if (op == 0) {
|
if (op == 0) {
|
||||||
result = Value(v1.getInt(cxt) * v2.getInt(cxt));
|
result = v1.getInt(cxt) * v2.getInt(cxt);
|
||||||
} else if (op == 1) {
|
} else if (op == 1) {
|
||||||
result = Value(v1.getInt(cxt) % v2.getInt(cxt));
|
result = v1.getInt(cxt) % v2.getInt(cxt);
|
||||||
} else if (op == 2) {
|
} else if (op == 2) {
|
||||||
result = Value(v1.getInt(cxt) + v2.getInt(cxt));
|
result = v1.getInt(cxt) + v2.getInt(cxt);
|
||||||
} else if (op == 3) {
|
} else if (op == 3) {
|
||||||
result = Value(v1.getInt(cxt) - v2.getInt(cxt));
|
result = v1.getInt(cxt) - v2.getInt(cxt);
|
||||||
} else if (op == 4) {
|
} else if (op == 4) {
|
||||||
result = Value(v1.getInt(cxt) & v2.getInt(cxt));
|
result = v1.getInt(cxt) & v2.getInt(cxt);
|
||||||
} else if (op == 5) {
|
} else if (op == 5) {
|
||||||
result = Value(v1.getInt(cxt) ^ v2.getInt(cxt));
|
result = v1.getInt(cxt) ^ v2.getInt(cxt);
|
||||||
} else if (op == 6) {
|
} else if (op == 6) {
|
||||||
result = Value(v1.getInt(cxt) | v2.getInt(cxt));
|
result = v1.getInt(cxt) | v2.getInt(cxt);
|
||||||
} else if (op == 7) {
|
} else if (op == 7) {
|
||||||
result = v2;
|
result = v2;
|
||||||
}
|
}
|
||||||
@ -141,7 +141,7 @@ Value Context::eval(peg::Ast& ast) {
|
|||||||
}
|
}
|
||||||
} else if (ast.name == "Block") {
|
} else if (ast.name == "Block") {
|
||||||
// TODO create new scope
|
// TODO create new scope
|
||||||
Value result = Value(Nil());
|
Value result = Nil();
|
||||||
for (int i=0; i<ast.nodes.size(); i++) {
|
for (int i=0; i<ast.nodes.size(); i++) {
|
||||||
result = eval(*nodes[i]);
|
result = eval(*nodes[i]);
|
||||||
}
|
}
|
||||||
|
142
src/types.cpp
142
src/types.cpp
@ -53,85 +53,141 @@ Value::Value(const Identifier &identifier) : type(TypeIdentifier) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(const Value &val) {
|
Value::Value(const Value &val) {
|
||||||
type = val.type;
|
if (val.isNil()) {
|
||||||
if (isNil()) {
|
*this = Nil();
|
||||||
value.nil = Nil();
|
} else if (val.isInt()) {
|
||||||
} else if (isInt()) {
|
*this = val.value.int_;
|
||||||
value.int_ = val.value.int_;
|
} else if (val.isFloat()) {
|
||||||
} else if (isFloat()) {
|
*this = val.value.float_;
|
||||||
value.float_ = val.value.float_;
|
} else if (val.isBoolean()) {
|
||||||
} else if (isBoolean()) {
|
*this = val.value.bool_;
|
||||||
value.bool_ = val.value.bool_;
|
} else if (val.isString()) {
|
||||||
} else if (isString()) {
|
*this = *val.value.string;
|
||||||
value.string = new std::string(*val.value.string);
|
} else if (val.isFunction()) {
|
||||||
} else if (isFunction()) {
|
*this = *val.value.function;
|
||||||
value.function = new Func(*val.value.function);
|
} else if (val.isArgs()) {
|
||||||
} else if (isArgs()) {
|
*this = *val.value.args;
|
||||||
value.args = new Args(*val.value.args);
|
} else if (val.isIdentifier()) {
|
||||||
} else if (isIdentifier()) {
|
*this = *val.value.identifier;
|
||||||
value.identifier = new Identifier(*val.value.identifier);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Value& Value::operator=(const Value &rhs) {
|
Value& Value::operator=(const Value &rhs) {
|
||||||
type = rhs.type;
|
cleanup();
|
||||||
if (isNil()) {
|
if (rhs.isNil()) {
|
||||||
value.nil = Nil();
|
*this = Nil();
|
||||||
} else if (isInt()) {
|
} else if (rhs.isInt()) {
|
||||||
value.int_ = rhs.value.int_;
|
*this = rhs.value.int_;
|
||||||
} else if (isFloat()) {
|
} else if (rhs.isFloat()) {
|
||||||
value.float_ = rhs.value.float_;
|
*this = rhs.value.float_;
|
||||||
} else if (isBoolean()) {
|
} else if (rhs.isBoolean()) {
|
||||||
value.bool_ = rhs.value.bool_;
|
*this = rhs.value.bool_;
|
||||||
} else if (isString()) {
|
} else if (rhs.isString()) {
|
||||||
value.string = new std::string(*rhs.value.string);
|
*this = *rhs.value.string;
|
||||||
} else if (isFunction()) {
|
} else if (rhs.isFunction()) {
|
||||||
value.function = new Func(*rhs.value.function);
|
*this = *rhs.value.function;
|
||||||
} else if (isArgs()) {
|
} else if (rhs.isArgs()) {
|
||||||
value.args = new Args(*rhs.value.args);
|
*this = *rhs.value.args;
|
||||||
} else if (isIdentifier()) {
|
} else if (rhs.isIdentifier()) {
|
||||||
value.identifier = new Identifier(*rhs.value.identifier);
|
*this = *rhs.value.identifier;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::~Value() {
|
Value::~Value() {
|
||||||
|
cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Value::cleanup() {
|
||||||
if (isString()) {
|
if (isString()) {
|
||||||
|
std::cerr << "string" << std::endl;
|
||||||
delete value.string;
|
delete value.string;
|
||||||
} else if (isFunction()) {
|
} else if (isFunction()) {
|
||||||
|
std::cerr << "func" << std::endl;
|
||||||
delete value.function;
|
delete value.function;
|
||||||
} else if (isArgs()) {
|
} else if (isArgs()) {
|
||||||
|
std::cerr << "args" << std::endl;
|
||||||
delete value.args;
|
delete value.args;
|
||||||
} else if (isIdentifier()) {
|
} else if (isIdentifier()) {
|
||||||
|
std::cerr << "identifier" << std::endl;
|
||||||
delete value.identifier;
|
delete value.identifier;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Value::isNil() {
|
Value& Value::operator=(Nil rhs) {
|
||||||
|
// cleanup();
|
||||||
|
type = TypeNil;
|
||||||
|
value.nil = rhs;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Value& Value::operator=(long rhs) {
|
||||||
|
// cleanup();
|
||||||
|
type = TypeInt;
|
||||||
|
value.int_ = rhs;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Value& Value::operator=(double rhs) {
|
||||||
|
// cleanup();
|
||||||
|
type = TypeFloat;
|
||||||
|
value.float_ = rhs;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Value& Value::operator=(bool rhs) {
|
||||||
|
// cleanup();
|
||||||
|
type = TypeBoolean;
|
||||||
|
value.bool_ = rhs;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Value& Value::operator=(const std::string &rhs) {
|
||||||
|
// cleanup();
|
||||||
|
type = TypeString;
|
||||||
|
value.string = new std::string(rhs);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Value& Value::operator=(const Func &rhs) {
|
||||||
|
// cleanup();
|
||||||
|
type = TypeFunction;
|
||||||
|
value.function = new Func(rhs);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Value& Value::operator=(const Args &rhs) {
|
||||||
|
// cleanup();
|
||||||
|
type = TypeArgs;
|
||||||
|
value.args = new Args(rhs);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Value& Value::operator=(const Identifier &rhs) {
|
||||||
|
// cleanup();
|
||||||
|
type = TypeIdentifier;
|
||||||
|
value.identifier = new Identifier(rhs);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Value::isNil() const {
|
||||||
return type == TypeNil;
|
return type == TypeNil;
|
||||||
}
|
}
|
||||||
bool Value::isInt() {
|
bool Value::isInt() const {
|
||||||
return type == TypeInt;
|
return type == TypeInt;
|
||||||
}
|
}
|
||||||
bool Value::isFloat() {
|
bool Value::isFloat() const {
|
||||||
return type == TypeFloat;
|
return type == TypeFloat;
|
||||||
}
|
}
|
||||||
bool Value::isNumber() {
|
bool Value::isNumber() const {
|
||||||
return isInt() || isFloat();
|
return isInt() || isFloat();
|
||||||
}
|
}
|
||||||
bool Value::isBoolean() {
|
bool Value::isBoolean() const {
|
||||||
return type == TypeBoolean;
|
return type == TypeBoolean;
|
||||||
}
|
}
|
||||||
bool Value::isString() {
|
bool Value::isString() const {
|
||||||
return type == TypeString;
|
return type == TypeString;
|
||||||
}
|
}
|
||||||
bool Value::isFunction() {
|
bool Value::isFunction() const {
|
||||||
return type == TypeFunction;
|
return type == TypeFunction;
|
||||||
}
|
}
|
||||||
bool Value::isArgs() {
|
bool Value::isArgs() const {
|
||||||
return type == TypeArgs;
|
return type == TypeArgs;
|
||||||
}
|
}
|
||||||
bool Value::isIdentifier() {
|
bool Value::isIdentifier() const {
|
||||||
return type == TypeIdentifier;
|
return type == TypeIdentifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
29
src/types.h
29
src/types.h
@ -96,15 +96,26 @@ public:
|
|||||||
Value& operator=(const Value &rhs);
|
Value& operator=(const Value &rhs);
|
||||||
~Value();
|
~Value();
|
||||||
|
|
||||||
bool isNil();
|
void cleanup();
|
||||||
bool isInt();
|
|
||||||
bool isFloat();
|
Value& operator=(Nil rhs);
|
||||||
bool isNumber();
|
Value& operator=(long rhs);
|
||||||
bool isBoolean();
|
Value& operator=(double rhs);
|
||||||
bool isString();
|
Value& operator=(bool rhs);
|
||||||
bool isFunction();
|
Value& operator=(const std::string &rhs);
|
||||||
bool isArgs();
|
Value& operator=(const Func &rhs);
|
||||||
bool isIdentifier();
|
Value& operator=(const Args &rhs);
|
||||||
|
Value& operator=(const Identifier &rhs);
|
||||||
|
|
||||||
|
bool isNil() const;
|
||||||
|
bool isInt() const;
|
||||||
|
bool isFloat() const;
|
||||||
|
bool isNumber() const;
|
||||||
|
bool isBoolean() const;
|
||||||
|
bool isString() const;
|
||||||
|
bool isFunction() const;
|
||||||
|
bool isArgs() const;
|
||||||
|
bool isIdentifier() const;
|
||||||
|
|
||||||
void assertNil(Context &cxt);
|
void assertNil(Context &cxt);
|
||||||
void assertInt(Context &cxt);
|
void assertInt(Context &cxt);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user