Skip to content

Commit

Permalink
moved peek, raise and lower stack to microcode, some enhancements to …
Browse files Browse the repository at this point in the history
…traits
  • Loading branch information
Jakz committed Feb 22, 2020
1 parent ab9a297 commit 5e2b02b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class String final : public TCollection, public Traits::Indexable, public Traits
{
public:
using data_t = std::string;
using utype_t = data_t;

private:
data_t value;
Expand Down
4 changes: 1 addition & 3 deletions src/help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ void Help::init()
addOperator("!", OpHelpEntry::unary(TYPE_LAMBDA, TYPE_UNKNOWN, "apply", Topic::FUNCTIONAL, "pop first value from stack and executes it as a lambda", "1 [2+] !", "3"));
addOperator("!", OpHelpEntry::unary(TYPE_INT, TYPE_FLOAT, "factorial", Topic::NUMERICS, "computes factorial", "", ""));
addOperator("=", OpHelpEntry::binary(TYPE_GENERIC, TYPE_GENERIC, TYPE_BOOL, "eq", Topic::LOGIC, "push true on stack if two topmost values are equal", "", ""));

addOperator("^", OpHelpEntry::unary(TYPE_GENERIC, "peek", Topic::UTILITY, "prints stack topmost value", "", ""));


addOperator("s", OpHelpEntry::unary(TYPE_GENERIC, TYPE_STRING, "string cast", Topic::TEXT, "convert the value to string", "", ""));

addOperator("?", OpHelpEntry::binary(TYPE_BOOL, TYPE_LAMBDA, TYPE_UNKNOWN, "if", Topic::LOGIC, "executes lambda if boolean value is true", "", ""));
Expand Down
15 changes: 1 addition & 14 deletions src/instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,6 @@ void Instruction::execute(VM *vm) const
break;
}

case OP_PEEK:
{
Value v = vm->peek();

if (v.valid())
std::cout << v.svalue();

break;
}

case OP_RAND:
{
if (vm->popOne(v1))
Expand Down Expand Up @@ -561,10 +551,7 @@ void Instruction::execute(VM *vm) const
}
break;
}

case OP_RAISE_STACK: { vm->raiseStack(); break; }
case OP_LOWER_STACK: { vm->lowerStack(); break; }


case OP_RAISE_STACKV:
{
if (vm->popOne(v1) && v1.type == TYPE_INT)
Expand Down
10 changes: 7 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ int main (int argc, const char * argv[])
else if (input == "info")
{
cout << " arch: " << (sizeof(void*) == 4 ? "x86" : "x86-64") << endl;
cout << " size of value : " << sizeof(Value) << (sizeof(Value) == sizeof(value_data) + sizeof(Type) ? " (packed)" : "") << endl;
cout << " size of instruction : " << sizeof(Instruction) << endl;
cout << " size of opcode : " << sizeof(Opcode) << endl;
cout << " size of value : " << sizeof(Value) << (sizeof(Value) == sizeof(value_data) + sizeof(Type) ? " (packed)" : "") << endl;
cout << " size of instruction : " << sizeof(Instruction) << endl;
cout << " size of opcode : " << sizeof(Opcode) << endl;
cout << " known types : " << TypeTraits::traits().size() << endl;
cout << " known operators : " << Opcode::OPCODES_COUNT << endl;
cout << " implemented operators : " << mc.vocabulary().size() << endl;

}
else
{
Expand Down
27 changes: 26 additions & 1 deletion src/microcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ void registerTernary(MicroCode& mc, Topic topic, const std::string& name, const
Help::addOperator(signature.opcode, signature.args, retn, topic, name, desc, examples);
}

template<typename T>
void reverseCollection(VM* vm, const Value& v)
{
const auto& data = v->as<T>->raw();
auto nv = T::utype_t(data.rbegin(), data.rend());
vm->push(new T(nv));
}

void registerFunctions(MicroCode& mc)
{
registerBinary(mc,
Expand Down Expand Up @@ -666,7 +674,24 @@ void registerStackFunctions(MicroCode& mc)
{ OP_DROP, TRAIT_ANY_TYPE }, { },
[] (VM* vm, const Value& v) { }
);


registerUnary(mc,
Topic::UTILITY, "peek", "prints stack topmost value",
{},
{ OP_PEEK, TRAIT_ANY_TYPE }, { },
[](VM* vm, const Value& v) {
/* TODO: optimize by avoiding popping and pushing */
if (v.valid())
std::cout << v.svalue();

vm->push(v);

}
);

registerNullary(mc, Topic::UTILITY, "raise-stack", "switch to higher stack", {}, { OP_RAISE_STACK }, { }, [](VM* vm) { vm->raiseStack(); });
registerNullary(mc, Topic::UTILITY, "lower-stack", "switch to lower stack", {}, { OP_LOWER_STACK }, { }, [](VM* vm) { vm->lowerStack(); });

//TODO: check return signature for rise and sink
registerTernary(mc,
Topic::STACK, "rise", "cycle top three stack element left",
Expand Down
16 changes: 15 additions & 1 deletion src/types/traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static const CollectionPrinter<Value> TuplePrinter = { "[[", "]]", " ", [] (cons

static const auto unary_false = [](const Value&) { return false; };
static const auto binary_false = [](const Value&, const Value&) { return false; };
static const auto to_zero = [](const Value&) { return 0; };
static const auto null_hasher = [](const Value&) { return 0UL; };
static const auto default_collector = [](size_t hint) { return std::make_pair(TYPE_NONE, nullptr); };

Expand All @@ -71,6 +72,7 @@ const std::unordered_map<Type, TypeTraits::TypeSpec, enum_hash> TypeTraits::spec
[] (const Value& v1, const Value& v2) { return v2.type == TYPE_INT && v2.data.i == v1.data.i; },
[] (const Value& v) { return std::hash<integral_t>()(v.integral()); },
unary_false,
[](const Value& v) { return v.integral(); },
default_collector,
}
},
Expand All @@ -83,7 +85,8 @@ const std::unordered_map<Type, TypeTraits::TypeSpec, enum_hash> TypeTraits::spec
},
[] (const Value& v1, const Value& v2) { return v2.type == TYPE_FLOAT && v2.data.f == v1.data.f; },
[] (const Value& v) { return std::hash<real_t>()(v.real()); },
unary_false
unary_false,
[](const Value& v) { return (int)v.real(); }
}
},
{ TYPE_BOOL,
Expand Down Expand Up @@ -112,6 +115,7 @@ const std::unordered_map<Type, TypeTraits::TypeSpec, enum_hash> TypeTraits::spec
[] (const Value& v1, const Value& v2) { return v2.type == TYPE_STRING && v2.string()->raw() == v1.string()->raw(); },
[] (const Value& v) { return std::hash<std::string>()(v.string()->raw()); },
unary_false,
to_zero,
[] (size_t hint) { return std::make_pair(TYPE_STRING, new String()); }
}
},
Expand Down Expand Up @@ -156,6 +160,7 @@ const std::unordered_map<Type, TypeTraits::TypeSpec, enum_hash> TypeTraits::spec
binary_false,
null_hasher,
unary_false,
to_zero,
[] (size_t hint) { return std::make_pair(TYPE_ARRAY, new Array()); }
}
},
Expand All @@ -166,6 +171,7 @@ const std::unordered_map<Type, TypeTraits::TypeSpec, enum_hash> TypeTraits::spec
[] (const Value& v1, const Value& v2) { return v2.type == TYPE_LIST && v2.list()->raw() == v1.list()->raw(); },
null_hasher,
unary_false,
to_zero,
[] (size_t hint) { return std::make_pair(TYPE_LIST, new List()); }
}
},
Expand All @@ -176,6 +182,7 @@ const std::unordered_map<Type, TypeTraits::TypeSpec, enum_hash> TypeTraits::spec
binary_false,
null_hasher,
unary_false,
to_zero,
[] (size_t hint) { return std::make_pair(TYPE_ARRAY, new Array()); }
}
},
Expand All @@ -186,6 +193,7 @@ const std::unordered_map<Type, TypeTraits::TypeSpec, enum_hash> TypeTraits::spec
binary_false,
null_hasher,
unary_false,
to_zero,
[] (size_t hint) { return std::make_pair(TYPE_SET, new Set()); }
}
},
Expand All @@ -195,6 +203,7 @@ const std::unordered_map<Type, TypeTraits::TypeSpec, enum_hash> TypeTraits::spec
[] (const Value& v1, const Value& v2) { return v2.type == TYPE_STACK && v2.list()->raw() == v1.list()->raw(); },
null_hasher,
unary_false,
to_zero,
[] (size_t hint) { return std::make_pair(TYPE_LIST, new List()); }
}
},
Expand All @@ -204,6 +213,7 @@ const std::unordered_map<Type, TypeTraits::TypeSpec, enum_hash> TypeTraits::spec
[] (const Value& v1, const Value& v2) { return v2.type == TYPE_QUEUE && v2.list()->raw() == v1.list()->raw(); },
null_hasher,
unary_false,
to_zero,
[] (size_t hint) { return std::make_pair(TYPE_LIST, new List()); }
}
},
Expand All @@ -221,6 +231,7 @@ const std::unordered_map<Type, TypeTraits::TypeSpec, enum_hash> TypeTraits::spec
binary_false,
null_hasher,
unary_false,
to_zero,
[] (size_t hint) { return std::make_pair(TYPE_ARRAY, new Array()); }
}
},
Expand Down Expand Up @@ -355,6 +366,9 @@ const std::unordered_map<Type, TypeTraits::TypeSpec, enum_hash> TypeTraits::spec
case OP_RECUR: return "[#]";
case OP_RECURV: return "{#}";
case OP_WHILE: return "<>";

case OP_RAISE_STACK: return "R";
case OP_LOWER_STACK: return "L";
}

assert(false);
Expand Down
1 change: 1 addition & 0 deletions src/types/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class TypeTraits
std::function<size_t(const Value&)> hasher;

std::function<bool(const Value&)> to_bool = [] (const Value& ) { return false; };
std::function<int(const Value&)> to_int = [](const Value&) { return 0; };

std::function<std::pair<Type, Traits::Appendable*>(size_t)> to_collector = [] (size_t) { return std::make_pair(TYPE_NONE, nullptr); };

Expand Down

0 comments on commit 5e2b02b

Please sign in to comment.