Skip to content

Commit

Permalink
better closure impl!
Browse files Browse the repository at this point in the history
  • Loading branch information
tokuhirom committed Mar 23, 2012
1 parent 19b13c0 commit eb38f04
Show file tree
Hide file tree
Showing 31 changed files with 497 additions and 136 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -25,3 +25,5 @@ callgrind.out.*
src/*/*.o-*
src/*.o-*
hoge.pl
hoge.cc
a.out
3 changes: 3 additions & 0 deletions BUGS
Expand Up @@ -8,6 +8,8 @@ KNOWN BUGS LIST
* 3/0.5 segv
* zero divided error
* close directory at destructor
* my $i=4; my $a = -> { $i+=4; }; $a.(undef); p($i)
* (-> { 3 }).();

__END__
use Test::More *;
Expand All @@ -29,3 +31,4 @@ class Foo {
is($Foo::Bar, 4649);

done_testing;

2 changes: 1 addition & 1 deletion SConstruct
Expand Up @@ -80,7 +80,7 @@ libfiles = [
node.cc op.cc vm.cc util.cc
symbol_table.cc package_map.cc frame.cc package.cc operator.cc
builtin.cc
object.cc
object.cc pad_list.cc
ops.gen.cc token.gen.cc lexer.gen.cc vm.gen.cc nodes.gen.cc symbols.gen.cc
Expand Down
2 changes: 2 additions & 0 deletions TODO
Expand Up @@ -55,6 +55,8 @@ glob('src/object/*.cc')
Test::LongString
Test::Fatal

File::which

syntax
------

Expand Down
2 changes: 1 addition & 1 deletion lib/Test/More.tra
Expand Up @@ -51,7 +51,7 @@ sub done_testing() {
say('# No tests run!');
}
if ($failed > 0) {
printf("# Looks like you failed %d test of %d.", $failed, $counter);
# printf("# Looks like you failed %d test of %d.", $failed, $counter);
}
}

1 change: 1 addition & 0 deletions t/tra/oop/destroy.tra
Expand Up @@ -6,6 +6,7 @@ class Foo {
self.bless(4649);
}
sub DESTROY() {
say("DESTROY");
$n = ${self};
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/test_compiler.cc
@@ -0,0 +1,20 @@
#include "nanotap.h"
#include "../tora/compiler.h"
#include "../tora/symbol_table.h"
#include "../tora/value.h"
#include "../tora/node.h"
#include <stdarg.h>

using namespace tora;

int main() {
{
SharedPtr<SymbolTable> st = new SymbolTable();
Compiler compiler(st, "<eval>");
SharedPtr<Node> n = new FuncdefNode(new StrNode(NODE_IDENTIFIER, "hoge"), new ListNode(), new Node(NODE_VOID));
compiler.compile(n);
}

done_testing();
}

63 changes: 63 additions & 0 deletions tests/test_lexical_frame.cc
@@ -0,0 +1,63 @@
#include "nanotap.h"
#include "../tora/frame.h"
#include "../tora/vm.h"
#include "../tora/symbol_table.h"
#include "../tora/op_array.h"
#include <stdarg.h>
#include <assert.h>

using namespace tora;

int main() {
{
SharedPtr<OPArray> opa(new OPArray());
SharedPtr<SymbolTable> st(new SymbolTable());
VM vm(opa, st, false);
int vars_cnt = 3;
int top = 0;
// LexicalVarsFrame(VM *vm, int vars_cnt, size_t top, frame_type_t type_=FRAME_TYPE_LEXICAL);
SharedPtr<LexicalVarsFrame> f1(new LexicalVarsFrame(&vm, vars_cnt, top));
vm.frame_stack->push_back(f1);
SharedPtr<LexicalVarsFrame> f2(new LexicalVarsFrame(&vm, vars_cnt, top));
vm.frame_stack->push_back(f2);
SharedPtr<Value> v = f2->get_variable_dynamic(1, 0);
is(v->value_type, VALUE_TYPE_UNDEF);
}

{
SharedPtr<OPArray> opa(new OPArray());
SharedPtr<SymbolTable> st(new SymbolTable());
VM vm(opa, st, false);
int vars_cnt = 3;
int top = 0;
// LexicalVarsFrame(VM *vm, int vars_cnt, size_t top, frame_type_t type_=FRAME_TYPE_LEXICAL);
SharedPtr<LexicalVarsFrame> f1(new LexicalVarsFrame(&vm, vars_cnt, top));
vm.frame_stack->push_back(f1);
f1->set_variable(0, new IntValue(5));
is(f1->get_variable(0)->to_int(), 5);
f1->set_variable(0, new IntValue(9));
is(f1->get_variable(0)->to_int(), 9);
}

{
SharedPtr<OPArray> opa(new OPArray());
SharedPtr<SymbolTable> st(new SymbolTable());
VM vm(opa, st, false);
int vars_cnt = 3;
int top = 0;
// LexicalVarsFrame(VM *vm, int vars_cnt, size_t top, frame_type_t type_=FRAME_TYPE_LEXICAL);
SharedPtr<LexicalVarsFrame> f1(new LexicalVarsFrame(&vm, vars_cnt, top));
vm.frame_stack->push_back(f1);
SharedPtr<LexicalVarsFrame> f2(new LexicalVarsFrame(&vm, vars_cnt, top));
vm.frame_stack->push_back(f2);
f2->set_variable(0, new IntValue(4));
f1->set_variable(0, new IntValue(5));
is(f2->get_variable(0)->to_int(), 4);
is(f2->get_variable_dynamic(1, 0)->to_int(), 5);
SharedPtr<Value> v = new StrValue("OOO");
f2->set_variable_dynamic(1, 0, v);
is(f2->get_variable_dynamic(1, 0)->to_s()->str_value(), v->to_s()->str_value());
}

done_testing();
}
4 changes: 3 additions & 1 deletion tests/test_package.cc
Expand Up @@ -11,7 +11,9 @@ static SharedPtr<Value> foo_hoge(VM *vm) {

int main() {
SharedPtr<SymbolTable> st = new SymbolTable();
SharedPtr<Package> pkg = new Package(st->get_id("Foo"));
SharedPtr<OPArray> op_array = new OPArray();
VM vm(op_array, st, false);
SharedPtr<Package> pkg = new Package(&vm, st->get_id("Foo"));

pkg->add_method(st->get_id("hoge"), new CallbackFunction(foo_hoge));
ok(pkg->has_method(st->get_id("hoge")));
Expand Down
53 changes: 53 additions & 0 deletions tests/test_pad_list.cc
@@ -0,0 +1,53 @@
#include "nanotap.h"
#include "../tora/frame.h"
#include <stdarg.h>
#include <assert.h>

using namespace tora;

int main() {
std::string filename_("foo.tra");

{
SharedPtr<PadList> p1 = new PadList(1, NULL);
p1->set(0, new IntValue(5));
SharedPtr<Value> val = p1->get(0);
ok(!!val.get());
is(val->value_type, VALUE_TYPE_INT);
is(val->to_int(), 5);

SharedPtr<Value> val1 = p1->get(1);
is(val1->value_type, VALUE_TYPE_UNDEF);
}

printf("# second phase\n");
{
SharedPtr<PadList> p1 = new PadList(0, NULL);
SharedPtr<PadList> p2 = new PadList(0, p1.get());
SharedPtr<PadList> p3 = new PadList(0, p2.get());
is((void*)p1->upper(1), (void*)NULL);
is(p2->upper(1), p1.get());
is(p3->upper(1), p2.get());
is(p3->upper(2), p1.get());
is((void*)p3->upper(3), (void*)NULL);
}

printf("# third phase\n");

{
SharedPtr<PadList> p1 = new PadList(1, NULL);
SharedPtr<PadList> p2 = new PadList(1, p1.get());
SharedPtr<PadList> p3 = new PadList(3, p2.get());
p1->set(0, new IntValue(5));
p2->set(1, new IntValue(9));
p3->set(2, new IntValue(6));
p3->set(0, new IntValue(3));

is(p2->get_dynamic(1, 0)->value_type, VALUE_TYPE_INT);
is(p2->get_dynamic(1, 0)->to_int(), 5);
is(p3->get_dynamic(2, 0)->to_int(), 5);
is(p3->get_dynamic(1, 1)->to_int(), 9);
}

done_testing();
}
28 changes: 18 additions & 10 deletions tests/test_value_code.cc
Expand Up @@ -2,20 +2,28 @@
#include "../tora/value/code.h"
#include "../tora/symbol_table.h"
#include <stdarg.h>
#include <vector>
#include <string>
#include <boost/shared_ptr.hpp>

using namespace tora;

int main() {
SymbolTable symbol_table;
std::string filename_("foo.tra");
int lineno = 3;
{
SharedPtr<SymbolTable> symbol_table = new SymbolTable();
std::string filename_("<eval>");
int lineno = 3;
boost::shared_ptr<std::vector<std::string>> params(new std::vector<std::string>());

SharedPtr<CodeValue> code = new CodeValue(
symbol_table->get_id("HOGE"), // package id
symbol_table->get_id("FUGA"), // func name id
filename_,
lineno,
params
);
is(code->package_id(), symbol_table->get_id("HOGE"));
}

SharedPtr<CodeValue> code = new CodeValue(
symbol_table.get_id("HOGE"), // package id
symbol_table.get_id("FUGA"), // func name id
filename_,
lineno
);
is(code->package_id(), symbol_table.get_id("HOGE"));
done_testing();
}

0 comments on commit eb38f04

Please sign in to comment.