Skip to content

Commit

Permalink
Started work on prototypes, indexing now allows expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndreynolds committed Sep 16, 2012
1 parent 5e4b42f commit d935a99
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 51 deletions.
14 changes: 8 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
# --------
# flathead

COMPILER=gcc
CC=gcc
CFLAGS=-Wall -O
TFLAGS=
YACC=bison -y -d -t -v
LEX=flex

MAIN=src/flathead.c
SRC_FILES=src/nodes.c src/gc.c src/eval.c src/runtime.c
LIB_FILES=lib/console.c lib/Math.c lib/Number.c lib/Object.c

OUT_FILE=-o bin/fh
GRAMMAR_FILE=src/grammar.y
LEX_FILE=src/lexer.l
Expand All @@ -17,6 +20,9 @@ YACC_OUT=y.tab.c

all: clean default

debug: CFLAGS += -g
debug: default

tests:
node test/runner.js
node ctest/crunner.js
Expand All @@ -30,12 +36,8 @@ lexer:
clean:
rm -rf y.* lex.yy.c bin/fh* a.out

debug: grammar lexer
$(COMPILER) -g $(OUT_FILE) $(YACC_OUT) $(LEX_OUT) $(MAIN) $(LIB_FILES) $(SRC_FILES) -lm
gdb bin/fh

install: default
cp bin/fh /usr/local/bin/

default: grammar lexer
$(COMPILER) $(OUT_FILE) $(YACC_OUT) $(LEX_OUT) $(MAIN) $(LIB_FILES) $(SRC_FILES) -lm
$(CC) $(CFLAGS) $(OUT_FILE) $(YACC_OUT) $(LEX_OUT) $(MAIN) $(LIB_FILES) $(SRC_FILES) -lm
58 changes: 58 additions & 0 deletions lib/Number.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,74 @@
#include <float.h>
#include "Number.h"

JSValue *
number_proto_to_exponential(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

JSValue *
number_proto_to_fixed(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

JSValue *
number_proto_to_locale_string(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

JSValue *
number_proto_to_precision(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

JSValue *
number_proto_to_string(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

JSValue *
number_proto_value_of(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

JSValue *
bootstrap_number()
{
JSValue *number = JSOBJ();
JSValue *prototype = JSOBJ();

// Number
// ------

// Properties
fh_set(number, "prototype", prototype);
fh_set(number, "MAX_VALUE", JSNUM(DBL_MAX));
fh_set(number, "MIN_VALUE", JSNUM(DBL_MIN));
fh_set(number, "NEGATIVE_INFINITY", JSNINF());
fh_set(number, "POSITIVE_INFINITY", JSINF());
fh_set(number, "NaN", JSNAN());

// Number.prototype
// ----------------

fh_set(prototype, "toExponential", JSNFUNC(&number_proto_to_exponential));
fh_set(prototype, "toFixed", JSNFUNC(&number_proto_to_fixed));
fh_set(prototype, "toLocaleString", JSNFUNC(&number_proto_to_locale_string));
fh_set(prototype, "toPrecision", JSNFUNC(&number_proto_to_precision));
fh_set(prototype, "toString", JSNFUNC(&number_proto_to_string));
fh_set(prototype, "valueOf", JSNFUNC(&number_proto_value_of));

return number;
}
7 changes: 7 additions & 0 deletions lib/Number.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@

#include "../src/flathead.h"

JSValue * number_proto_to_exponential(JSArgs *, State *);
JSValue * number_proto_to_fixed(JSArgs *, State *);
JSValue * number_proto_to_locale_string(JSArgs *, State *);
JSValue * number_proto_to_precision(JSArgs *, State *);
JSValue * number_proto_to_string(JSArgs *, State *);
JSValue * number_proto_value_of(JSArgs *, State *);

JSValue * bootstrap_number(void);
23 changes: 20 additions & 3 deletions lib/Object.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,132 +9,149 @@ JSValue *
obj_create(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.defineProperty(obj, prop, descriptor)
JSValue *
obj_define_property(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.defineProperties(obj, props)
JSValue *
obj_define_properties(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.getOwnPropertyDescriptor(obj, prop)
JSValue *
obj_get_own_property_descriptor(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.keys(obj)
JSValue *
obj_keys(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.getOwnPropertyNames(obj)
JSValue *
obj_get_own_property_names(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.getPrototypeOf(obj)
JSValue *
obj_get_prototype_of(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.preventExtensions(obj)
JSValue *
obj_prevent_extensions(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.isExtensible(obj)
JSValue *
obj_is_extensible(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.seal(obj)
JSValue *
obj_seal(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.isSealed(obj)
JSValue *
obj_is_sealed(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.freeze(obj)
JSValue *
obj_freeze(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.isFrozen(obj)
JSValue *
obj_is_frozen(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.prototype.hasOwnProperty(prop)
JSValue *
obj_proto_has_own_property(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.prototype.isPrototypeOf(object)
JSValue *
obj_proto_is_prototype_of(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.prototype.propertyIsEnumerable(prop)
JSValue *
obj_proto_property_is_enumerable(JSArgs *args, State *state)
{
// TODO: stub
return JSUNDEF();
}

// Object.prototype.toLocaleString()
JSValue *
obj_proto_to_locale_string(JSArgs *args, State *state)
{
// TODO: stub
return obj_proto_to_string(args, state);
}

// Object.prototype.toString()
JSValue *
obj_proto_to_string(JSArgs *args, State *state)
{
// TODO: stub
return JSSTR("[object Object]");
}

// Object.prototype.valueOf()
JSValue *
obj_proto_value_of(JSArgs *args, State *state)
{
// TODO: stub
// TODO
return JSSTR("[object Object]");
}

JSValue *
Expand Down
1 change: 1 addition & 0 deletions lib/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ console_assert(JSArgs *args, State *state)
if (result->boolean.val) return JSUNDEF();
}
fh_error(state, E_ASSERTION, "assertion failed");
assert(0);
}

JSValue *
Expand Down
31 changes: 21 additions & 10 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fh_if(JSValue *ctx, Node *node)
return fh_eval(ctx, node->e2);
else if (node->e3 != NULL)
return fh_eval(ctx, node->e3);
return NULL;
}

JSValue *
Expand Down Expand Up @@ -159,23 +160,26 @@ fh_arr(JSValue *ctx, Node *node)
}

JSValue *
fh_str_from_ident(Node *ident)
fh_str_from_node(JSValue *ctx, Node *node)
{
return ident->sval != NULL ?
JSSTR(ident->sval) :
JSCAST(JSNUM(ident->val), T_STRING);
// Need to evaluate the node to a string key.
// e.g. obj['a'] obj.a arr[1] arr[arr.length - 1]

if (node->type == NODE_IDENT)
return JSSTR(node->sval);
return JSCAST(fh_eval(ctx, node), T_STRING);
}

JSValue *
fh_member(JSValue *ctx, Node *member)
{
JSValue *id1, *id2, *parent;
id1 = fh_str_from_ident(member->e1);
id2 = fh_str_from_ident(member->e2);
id1 = fh_str_from_node(ctx, member->e1);
id2 = fh_str_from_node(ctx, member->e2);
parent = member->e2->type == NODE_MEMBER ?
fh_member(ctx, member->e2) :
fh_get(ctx, id2->string.ptr);
return fh_get(parent, id1->string.ptr);
return fh_get_proto(parent, id1->string.ptr);
}

JSValue *
Expand Down Expand Up @@ -245,10 +249,8 @@ fh_setup_func_env(JSValue *ctx, JSValue *func, JSArgs *args)
{
JSValue *arguments = JSOBJ();
Node *func_node = func->function.node;
JSValue *scope = func->function.closure;
JSValue *scope = func->function.closure ? func->function.closure : JSOBJ();

if (scope == NULL)
scope = JSOBJ();
scope->object.parent = ctx;
fh_set(scope, "arguments", arguments);
if (func_node->sval != NULL)
Expand Down Expand Up @@ -307,6 +309,7 @@ fh_eval_postfix_exp(JSValue *ctx, Node *node)
fh_set(ctx, node->e1->sval, fh_sub(old_val, JSNUM(1)));
return old_val;
}
assert(0);
}

JSValue *
Expand Down Expand Up @@ -350,6 +353,8 @@ fh_eval_prefix_exp(JSValue *ctx, Node *node)
fh_set(ctx, node->e1->sval, new_val);
return new_val;
}

assert(0);
}

JSValue *
Expand Down Expand Up @@ -385,6 +390,8 @@ fh_eval_bin_exp(JSValue *ctx, Node *node)
return JSBOOL(fh_lt(a, b)->boolean.val || fh_eq(a, b, false)->boolean.val);
if (STREQ(op, ">="))
return JSBOOL(fh_gt(a, b)->boolean.val || fh_eq(a, b, false)->boolean.val);

assert(0);
}

JSValue *
Expand Down Expand Up @@ -418,6 +425,8 @@ fh_add(JSValue *a, JSValue *b)
// Number and Boolean => Number
if (T_XOR(a, b, T_NUMBER, T_BOOLEAN))
return fh_add(JSCAST(a, T_NUMBER), JSCAST(b, T_NUMBER));

assert(0);
}

JSValue *
Expand Down Expand Up @@ -516,6 +525,8 @@ fh_gt(JSValue *a, JSValue *b)
}
if (T_BOTH(a, b, T_STRING))
return JSBOOL(strcmp(a->string.ptr, b->string.ptr) > 0);

assert(0);
}

JSValue *
Expand Down
Loading

0 comments on commit d935a99

Please sign in to comment.