Skip to content

Commit

Permalink
moved # (append) to MicroCode, added Traits::Appendable behavior for …
Browse files Browse the repository at this point in the history
…List Array Stack Queue
  • Loading branch information
Jakz committed Jun 29, 2019
1 parent 7098f4a commit aea581c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 83 deletions.
5 changes: 4 additions & 1 deletion src/collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class List : public TCollection, public Traits::Iterable, public Traits::Appenda
public:
using list_t = std::list<Value>;
using utype_t = list_t;
private:
protected:
list_t data;

public:
Expand Down Expand Up @@ -196,13 +196,16 @@ class Stack : public List
Stack() : List() { }
Stack(const std::list<Value>& data) : List(data) { }

void put(Value value) override { data.push_front(value); }
};

class Queue : public List
{
public:
Queue() : List() { }
Queue(const std::list<Value>& data) : List(data) { }

void put(Value value) override { data.push_back(value); }
};

#pragma mark Array
Expand Down
87 changes: 15 additions & 72 deletions src/instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,41 +157,6 @@ void Instruction::execute(VM *vm) const
}
break;
}

case OP_QUESTION:
{
if (vm->popTwo(v1, v2))
{
{
switch (TYPES(v1.type, v2.type))
{
case TYPES(TYPE_BOOL,TYPE_LAMBDA):
{
if (v1.boolean())
vm->execute(v2.lambda()->code());
break;
}
}

}
}
break;
}
case OP_DQUESTION:
{
if (vm->popThree(v1, v2, v3))
{
if (v1.type == TYPE_BOOL && v2.type == TYPE_LAMBDA && v3.type == TYPE_LAMBDA)
{
if (v1.boolean())
vm->execute(v2.lambda()->code());
else
vm->execute(v3.lambda()->code());
}
}
break;
}


case OP_PEEK:
{
Expand Down Expand Up @@ -381,54 +346,32 @@ void Instruction::execute(VM *vm) const

case OP_HASH:
{
if (vm->popTwo(v2, v3))
if (vm->popThree(v1, v2, v3))
{
if (v2.type == TYPE_SET)
if (v1.type == TYPE_MAP)
{
Set *set = v2.set();
set->put(v3);
vm->push(v2);
}
else if (v2.type == TYPE_STACK)
{
v2.stack()->raw().push_front(v3);
vm->push(v2);
}
else if (v2.type == TYPE_QUEUE)
{
v2.queue()->raw().push_back(v3);
vm->push(v2);
v1.map()->raw().emplace(std::make_pair(v2, v3));
vm->push(v1);
}
else
{
if (vm->popOne(v1))
{
switch (TYPES(v1.type, v2.type))
{
if (v1.type == TYPE_MAP)
case TYPES(TYPE_ARRAY, TYPE_INT):
{
v1.map()->raw().emplace(std::make_pair(v2, v3));
vm->push(v1);
}
else
{
switch (TYPES(v1.type, v2.type))
{
case TYPES(TYPE_ARRAY, TYPE_INT):
{
auto& values = v1.array()->raw();
integral_t i = v2.integral();
auto& values = v1.array()->raw();
integral_t i = v2.integral();

if (i >= values.size() && i >= values.capacity())
values.resize(i+1, Value());
if (i >= values.size() && i >= values.capacity())
values.resize(i+1, Value());

values[v2.integral()] = v3;
values[v2.integral()] = v3;

vm->push(v1);
break;
}
}
vm->push(v1);
break;
}
}
}
}
}
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ int main (int argc, const char * argv[])
string input;
bool finished = false;

input = "(?0:[1]1:[1][1<>2<>+])2@";
/*input = "(?0:[1]1:[1][1<>2<>+])2@";
compiler::Compiler compiler;
Code*code = compiler.compile(input);
if (code)
{
vm.execute(code);
vm.printTopStack();
}
return 0;
return 0;*/

while (!finished)
{
Expand Down
17 changes: 13 additions & 4 deletions src/microcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using V = const Value&;
void registerStackFunctions(MicroCode& mc);
void registerNumericFunctions(MicroCode& mc);
void registerStringFunctions(MicroCode& mc);
void registerFlowFunctions(MicroCode& mc);

SignatureArguments normalize(const SignatureArguments& args)
{
Expand Down Expand Up @@ -324,6 +325,14 @@ void registerFunctions(MicroCode& mc)

vm->push(true);
});

registerBinary(mc, Topic::COLLECTIONS, "append", "add an element to an appendable collection, following semantics of the collection", {},
{ OP_HASH, TRAIT_ITERABLE, TRAIT_ANY_TYPE }, { TRAIT_ITERABLE },
[](VM* vm, V v1, V v2) {
Traits::Appendable* appendable = v1.appendable();
appendable->put(v2);
vm->pushCollection(v1);
});


std::initializer_list<std::tuple<Opcode, Opcode, Opcode, std::string>> embedded_helpers = {
Expand Down Expand Up @@ -482,11 +491,10 @@ void registerFunctions(MicroCode& mc)
registerUnary(mc, {OP_ANY, TYPE_TUPLE}, { TRAIT_ANY_TYPE }, [](VM* vm, V v1) { vm->push(v1.tuple()->at(0)); });
registerUnary(mc, {OP_EVERY, TYPE_TUPLE}, { TRAIT_ANY_TYPE }, [](VM* vm, V v1) { vm->push(v1.tuple()->at(1)); });



registerStackFunctions(mc);
registerNumericFunctions(mc);
registerStringFunctions(mc);
registerFlowFunctions(mc);
}

namespace math
Expand Down Expand Up @@ -646,10 +654,11 @@ void registerFlowFunctions(MicroCode& mc)
Topic::LOGIC, "if-else", "executes first lambdfa if value on stack is true, second lambda otherwise",
{},
{ OP_DQUESTION, TYPE_BOOL, TYPE_LAMBDA, TYPE_LAMBDA }, { },
[] (VM* vm, V v1, V v2, V v3) {
if (v1.boolean())
vm->execute(v2.lambda()->code());
vm->execute(v2.lambda()->code());
else
vm->execute(v3.lambda()->code());
vm->execute(v3.lambda()->code());
});
}

Expand Down
1 change: 1 addition & 0 deletions src/parser/impossible.l
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

"(?" { return Parser::make_LARRAY_OPEN(loc); }
"{>" { return Parser::make_STACK_OPEN(loc); }
"{<" { return Parser::make_QUEUE_OPEN(loc); }

"[[" { return Parser::make_TUPLE_OPEN(loc); }
"]]" { return Parser::make_TUPLE_CLOSE(loc); }
Expand Down
7 changes: 4 additions & 3 deletions src/parser/impossible.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

%defines
%define api.namespace {compiler}
%define parser_class_name {Parser}
%define api.parser.class {Parser}

%define api.token.constructor
%define api.value.type variant
Expand Down Expand Up @@ -66,6 +66,7 @@

LARRAY_OPEN "(?"
STACK_OPEN "{>"
QUEUE_OPEN "{<"

TUPLE_OPEN "[["
TUPLE_CLOSE "]]"
Expand Down Expand Up @@ -243,8 +244,8 @@ value:
| STACK_OPEN values RBRACE { $$ = new Stack($2); }
| STACK_OPEN RBRACE { $$ = new Stack(); }

| LBRACE LESSER values RBRACE { $$ = new Queue($3); }
| LBRACE LESSER RBRACE { $$ = new Queue(); }
| QUEUE_OPEN values RBRACE { $$ = new Queue($2); }
| QUEUE_OPEN RBRACE { $$ = new Queue(); }

| LBRACE PERIOD values RBRACE {
$$ = new Set(Set::set_t($3.begin(), $3.end()));
Expand Down
1 change: 1 addition & 0 deletions src/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Error* Value::error() const { assert(type == TYPE_ERROR); return object<Error>()

Traits::Indexable* Value::indexable() const { return object<Traits::Indexable>(); }
Traits::Iterable* Value::iterable() const { return object<Traits::Iterable>(); }
Traits::Appendable* Value::appendable() const { return object<Traits::Appendable>(); }


Value Value::INVALID = Value(TYPE_INVALID);
Expand Down
1 change: 1 addition & 0 deletions src/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class Value final

Traits::Indexable* indexable() const;
Traits::Iterable* iterable() const;
Traits::Appendable* appendable() const;

Lambda* lambda() const;

Expand Down
6 changes: 5 additions & 1 deletion src/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class VM
const MicroCode& microcode;

public:
VM(MicroCode& microcode) : valueStack(new stack_t()), exec(ActivationRecord()), running(false), stackPreserve(false), memory(),
VM(MicroCode& microcode) : valueStack(new stack_t()), exec(ActivationRecord()), running(false), stackPreserve(true), memory(),
microcode(microcode)
{
}
Expand All @@ -68,11 +68,15 @@ class VM
return exec.code;
}

bool preserveCollectionOnStack() const { return stackPreserve; }

void pushRecord(ActivationRecord&& record);
void popRecord();

const auto& record() const { return exec; }

void pushCollection(const Value& value) { if (preserveCollectionOnStack()) push(value); }

void push(const Value& value)
{
valueStack->push_back(value);
Expand Down

0 comments on commit aea581c

Please sign in to comment.