Minor cleanups.

This commit is contained in:
Tor Andersson
2014-01-16 02:14:07 +01:00
parent f7cdddbbe9
commit 18b192b8aa
9 changed files with 35 additions and 30 deletions
+8 -10
View File
@@ -11,15 +11,6 @@
typedef struct js_State js_State;
typedef struct js_StringNode js_StringNode;
typedef struct js_Ast js_Ast;
typedef struct js_Function js_Function;
typedef struct js_Environment js_Environment;
typedef struct js_Value js_Value;
typedef struct js_Object js_Object;
typedef struct js_Property js_Property;
typedef int (*js_CFunction)(js_State *J, int argc);
#define JS_REGEXP_G 1
@@ -34,8 +25,15 @@ int js_error(js_State *J, const char *fmt, ...);
int js_loadstring(js_State *J, const char *s);
int js_loadfile(js_State *J, const char *filename);
const char *js_intern(js_State *J, const char *s);
/* private */
typedef struct js_Ast js_Ast;
typedef struct js_Environment js_Environment;
typedef struct js_Function js_Function;
typedef struct js_Object js_Object;
typedef struct js_StringNode js_StringNode;
const char *js_intern(js_State *J, const char *s);
void js_printstringtree(js_State *J);
#endif
+5 -5
View File
@@ -15,8 +15,8 @@ static int paramlen(js_Ast *list)
{
int n = 0;
while (list) {
n++;
list = list->b;
++n;
}
return n;
}
@@ -54,7 +54,7 @@ static js_Function *newfun(js_State *J, js_Ast *name, js_Ast *params, js_Ast *bo
static void freefun(js_State *J, js_Function *F)
{
// int i;
// for (i = 0; i < F->funlen; i++)
// for (i = 0; i < F->funlen; ++i)
// freefun(J, F->funtab[i]);
free(F->funtab);
free(F->numtab);
@@ -87,7 +87,7 @@ static int addfunction(JF, js_Function *value)
static int addnumber(JF, double value)
{
int i;
for (i = 0; i < F->numlen; i++)
for (i = 0; i < F->numlen; ++i)
if (F->numtab[i] == value)
return i;
if (F->numlen >= F->numcap) {
@@ -101,7 +101,7 @@ static int addnumber(JF, double value)
static int addstring(JF, const char *value)
{
int i;
for (i = 0; i < F->strlen; i++)
for (i = 0; i < F->strlen; ++i)
if (!strcmp(F->strtab[i], value))
return i;
if (F->strlen >= F->strcap) {
@@ -224,7 +224,7 @@ static int cargs(JF, js_Ast *list)
while (list) {
cexp(J, F, list->a);
list = list->b;
n++;
++n;
}
return n;
}
-1
View File
@@ -111,7 +111,6 @@ js_Function *jsC_compile(js_State *J, js_Ast *prog);
void jsC_freecompile(js_State *J);
int jsC_error(js_State *J, js_Ast *node, const char *fmt, ...);
void jsC_dumpvalue(js_State *J, js_Value v);
void jsC_dumpfunction(js_State *J, js_Function *fun);
#endif
+5 -5
View File
@@ -596,16 +596,16 @@ void jsC_dumpfunction(js_State *J, js_Function *F)
int i;
printf("function %p %s(", F, F->name);
for (i = 0; i < F->numparams; i++)
for (i = 0; i < F->numparams; ++i)
printf("%s%s", i > 0 ? ", " : "", F->params[i]);
printf(")\n");
for (i = 0; i < F->funlen; i++)
for (i = 0; i < F->funlen; ++i)
printf("\tfunction %p %s\n", F->funtab[i], F->funtab[i]->name);
for (i = 0; i < F->strlen; i++) {
for (i = 0; i < F->strlen; ++i) {
ps("\tstring "); pstr(F->strtab[i]); ps("\n");
}
// TODO: regexp
for (i = 0; i < F->numlen; i++)
for (i = 0; i < F->numlen; ++i)
printf("\tnumber %.9g\n", F->numtab[i]);
while (p < end) {
@@ -648,7 +648,7 @@ void jsC_dumpfunction(js_State *J, js_Function *F)
nl();
}
for (i = 0; i < F->funlen; i++) {
for (i = 0; i < F->funlen; ++i) {
if (F->funtab[i] != F) {
nl();
jsC_dumpfunction(J, F->funtab[i]);
+1 -1
View File
@@ -71,7 +71,7 @@ static void printstringnode(js_StringNode *node, int level)
if (node->left != &sentinel)
printstringnode(node->left, level + 1);
printf("%d: ", node->level);
for (i = 0; i < level; i++)
for (i = 0; i < level; ++i)
putchar('\t');
printf("'%s'\n", node->string);
if (node->right != &sentinel)
+4 -1
View File
@@ -1,8 +1,11 @@
#ifndef js_object_h
#define js_object_h
typedef enum js_Class js_Class;
typedef enum js_Type js_Type;
typedef struct js_Value js_Value;
typedef enum js_Class js_Class;
typedef struct js_Property js_Property;
struct js_Environment
{
+10 -5
View File
@@ -13,7 +13,7 @@ static void js_call(js_State *J, int argc);
static void js_dumpstack(js_State *J)
{
int i;
for (i = 0; i < top; i++) {
for (i = 0; i < top; ++i) {
printf("stack %d: ", i);
js_dumpvalue(J, stack[i]);
putchar('\n');
@@ -502,15 +502,20 @@ static void runfun(js_State *J, js_Function *F, js_Environment *E)
js_pushboolean(J, x >= y);
break;
// case OP_EQ:
// case OP_NE:
case OP_EQ:
case OP_STRICTEQ:
x = js_tonumber(J, -2);
y = js_tonumber(J, -1);
js_pop(J, 2);
js_pushboolean(J, x == y);
break;
// case OP_STRICTNE:
case OP_NE:
case OP_STRICTNE:
x = js_tonumber(J, -2);
y = js_tonumber(J, -1);
js_pop(J, 2);
js_pushboolean(J, x != y);
break;
/* Branching */
@@ -564,7 +569,7 @@ static void js_call(js_State *J, int argc)
F = obj->function;
E = js_newenvironment(J, obj->scope, js_newobject(J, JS_COBJECT));
for (i = 0; i < F->numparams; i++) {
for (i = 0; i < F->numparams; ++i) {
ref = js_decvar(J, E, F->params[i]);
if (i + 1 < argc)
ref->value = js_tovalue(J, i + 1);
+1 -1
View File
@@ -1,6 +1,6 @@
#include "js.h"
#include "jsstate.h"
#include "jsobject.h"
#include "jsstate.h"
js_State *js_newstate(void)
{
+1 -1
View File
@@ -8,7 +8,7 @@ main(int argc, char **argv)
J = js_newstate();
for (i = 1; i < argc; i++) {
for (i = 1; i < argc; ++i) {
js_loadfile(J, argv[i]);
// js_run(J);
}