return tree;
}
-AST* String(char* val)
+AST* String(Tok* val)
{
AST* node = ast(AST_STRING);
- node->value.text = val;
+ node->value.text = (char*)gc_addref(val->value.text);
return node;
}
char* string_value(AST* val)
{
+ assert(val != NULL);
assert(val->type == AST_STRING);
return val->value.text;
}
-AST* Symbol(char* val)
+AST* Symbol(Tok* val)
{
AST* node = ast(AST_SYMBOL);
- node->value.text = val;
+ node->value.text = (char*)gc_addref(val->value.text);
return node;
}
char* symbol_value(AST* val)
{
+ assert(val != NULL);
assert(val->type == AST_SYMBOL);
return val->value.text;
}
-AST* Char(uint32_t val)
+AST* Char(Tok* val)
{
AST* node = ast(AST_CHAR);
- node->value.character = val;
+ node->value.character = val->value.character;
return node;
}
uint32_t char_value(AST* val)
{
+ assert(val != NULL);
assert(val->type == AST_CHAR);
return val->value.character;
}
-AST* Integer(intptr_t val)
+AST* Integer(Tok* val)
{
AST* node = ast(AST_INT);
- node->value.integer = val;
+ node->value.integer = val->value.integer;
return node;
}
intptr_t integer_value(AST* val)
{
+ assert(val != NULL);
assert(val->type == AST_INT);
return val->value.integer;
}
-AST* Float(double val)
+AST* Float(Tok* val)
{
AST* node = ast(AST_FLOAT);
- node->value.floating = val;
+ node->value.floating = val->value.floating;
return node;
}
double float_value(AST* val)
{
+ assert(val != NULL);
assert(val->type == AST_FLOAT);
return val->value.floating;
}
-AST* Bool(bool val)
+AST* Bool(Tok* val)
{
AST* node = ast(AST_BOOL);
- node->value.boolean = val;
+ node->value.boolean = val->value.boolean;
return node;
}
bool bool_value(AST* val)
{
+ assert(val != NULL);
assert(val->type == AST_BOOL);
return val->value.boolean;
}
-AST* Ident(char* val)
+AST* Ident(Tok* val)
{
AST* node = ast(AST_IDENT);
- node->value.text = val;
+ node->value.text = (char*)gc_addref(val->value.text);
return node;
}
char* ident_value(AST* val)
{
+ assert(val != NULL);
assert(val->type == AST_IDENT);
return val->value.text;
}
-AST* Require(char* name)
+AST* Require(Tok* name)
{
AST* node = ast(AST_REQ);
- node->value.text = name;
+ node->value.text = (char*)gc_addref(name->value.text);
return node;
}
char* require_name(AST* req)
{
+ assert(req != NULL);
assert(req->type == AST_REQ);
return req->value.text;
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-AST* Def(char* name, AST* value)
+AST* Def(Tok* name, AST* value)
{
- (void)name;
- (void)value;
- return NULL;
+ AST* node = ast(AST_DEF);
+ node->value.def.name = (char*)gc_addref(name->value.text);
+ node->value.def.value = (AST*)gc_addref(value);
+ return node;
}
char* def_name(AST* def)
{
- (void)def;
- return NULL;
+ assert(def != NULL);
+ assert(def->type == AST_DEF);
+ return def->value.def.name;
}
AST* def_value(AST* def)
{
- (void)def;
- return NULL;
-}
-
-AST* Ann(char* name, AST* value)
-{
- (void)name;
- (void)value;
- return NULL;
-}
-
-char* ann_name(AST* ann)
-{
- (void)ann;
- return NULL;
-}
-
-AST* ann_value(AST* ann)
-{
- (void)ann;
- return NULL;
-}
-
-AST* IfExpr(AST* cond, AST* bthen, AST* belse)
-{
- (void)cond;
- (void)bthen;
- (void)belse;
- return NULL;
-}
-
-AST* ifexpr_condition(AST* ifexpr)
-{
- (void)ifexpr;
- return NULL;
-}
-
-AST* ifexpr_branch_then(AST* ifexpr)
-{
- (void)ifexpr;
- return NULL;
-}
-
-AST* ifexpr_branch_else(AST* ifexpr)
-{
- (void)ifexpr;
- return NULL;
-}
-
-AST* Func(AST* args, AST* body)
-{
- (void)args;
- (void)body;
- return NULL;
-}
-
-AST* func_args(AST* func)
-{
- (void)func;
- return NULL;
-}
-
-AST* func_body(AST* func)
-{
- (void)func;
- return NULL;
-}
-
-AST* Block(void)
-{
- return NULL;
-}
-
-void block_append(AST* expr)
-{
- (void)expr;
-}
-
-size_t block_size(AST* block)
-{
- (void)block;
- return 0;
-}
-
-AST* block_get(size_t index)
-{
- (void)index;
- return NULL;
-}
+ assert(def != NULL);
+ assert(def->type == AST_DEF);
+ return def->value.def.value;
+}
+
+//AST* Ann(char* name, AST* value)
+//{
+// (void)name;
+// (void)value;
+// return NULL;
+//}
+//
+//char* ann_name(AST* ann)
+//{
+// (void)ann;
+// return NULL;
+//}
+//
+//AST* ann_value(AST* ann)
+//{
+// (void)ann;
+// return NULL;
+//}
+//
+//AST* IfExpr(AST* cond, AST* bthen, AST* belse)
+//{
+// (void)cond;
+// (void)bthen;
+// (void)belse;
+// return NULL;
+//}
+//
+//AST* ifexpr_condition(AST* ifexpr)
+//{
+// (void)ifexpr;
+// return NULL;
+//}
+//
+//AST* ifexpr_branch_then(AST* ifexpr)
+//{
+// (void)ifexpr;
+// return NULL;
+//}
+//
+//AST* ifexpr_branch_else(AST* ifexpr)
+//{
+// (void)ifexpr;
+// return NULL;
+//}
+//
+//AST* Func(AST* args, AST* body)
+//{
+// (void)args;
+// (void)body;
+// return NULL;
+//}
+//
+//AST* func_args(AST* func)
+//{
+// (void)func;
+// return NULL;
+//}
+//
+//AST* func_body(AST* func)
+//{
+// (void)func;
+// return NULL;
+//}
+//
+//AST* Block(void)
+//{
+// return NULL;
+//}
+//
+//void block_append(AST* expr)
+//{
+// (void)expr;
+//}
+//
+//size_t block_size(AST* block)
+//{
+// (void)block;
+// return 0;
+//}
+//
+//AST* block_get(size_t index)
+//{
+// (void)index;
+// return NULL;
+//}
if (ptr != NULL) {
obj->refs++;
if (obj->refs == 1) {
- lookup.object = obj;
+ lookup.object = ptr;
entry = hash_del(&Zero_Count_Table, &lookup);
assert(entry != NULL);
hash_set(&Multi_Ref_Table, entry);
if (ptr != NULL) {
obj->refs--;
if (obj->refs == 0) {
- lookup.object = obj;
+ lookup.object = ptr;
entry = hash_del(&Multi_Ref_Table, &lookup);
assert(entry != NULL);
hash_set(&Zero_Count_Table, entry);
#include <sclpl.h>
static AST* require(Parser* p);
+static AST* definition(Parser* p);
static AST* expression(Parser* p);
static AST* literal(Parser* p);
-static AST* expect_lit(Parser* p, TokType);
static AST* token_to_tree(Tok* tok);
AST* toplevel(Parser* p)
if (peek(p)->type != T_END_FILE) {
if (accept_str(p, T_ID, "require"))
ret = require(p);
+ else if (accept_str(p, T_ID, "def"))
+ ret = definition(p);
else
ret = expression(p);
}
return ret;
}
+static AST* definition(Parser* p)
+{
+ Tok* id = expect(p, T_ID);//expect_lit(p, T_ID);
+ AST* expr;
+ //if (peek(p)->type == T_LPAR)
+ // expr = function(p);
+ //else
+ expr = expression(p);
+ expect(p, T_END);
+ return Def(id, expr);
+}
+
+static AST* require(Parser* p)
+{
+ Tok* tok = expect(p, T_STRING);
+ AST* ast = Require(tok);
+ expect(p, T_END);
+ return ast;
+}
+
static AST* expression(Parser* p)
{
if (peek(p)->type == T_ID) {
- return expect_lit(p, T_ID);
+ return Ident(expect(p,T_ID)); //expect_lit(p, T_ID);
//if (peek(p)->type == T_LPAR) {
// arglist(p);
//}
//}
}
-static AST* require(Parser* p)
-{
- AST* ret = expect_lit(p, T_STRING);
- if (ret != NULL)
- ret = Require(string_value(ret));
- expect(p, T_END);
- return ret;
-}
-
static AST* literal(Parser* p)
{
AST* ret = NULL;
case T_STRING:
case T_INT:
case T_FLOAT:
- ret = token_to_tree(tok);
- expect(p, tok->type);
+ ret = token_to_tree(expect(p, tok->type));
break;
default:
error(p, "Expected a literal");
static AST* token_to_tree(Tok* tok)
{
switch (tok->type) {
- case T_BOOL: return Bool(tok->value.boolean);
- case T_CHAR: return Char(tok->value.character);
- case T_STRING: return String(tok->value.text);
- case T_INT: return Integer(tok->value.integer);
- case T_FLOAT: return Float(tok->value.floating);
- case T_ID: return Ident(tok->value.text);
+ case T_BOOL: return Bool(tok);
+ case T_CHAR: return Char(tok);
+ case T_STRING: return String(tok);
+ case T_INT: return Integer(tok);
+ case T_FLOAT: return Float(tok);
+ case T_ID: return Ident(tok);
default: return NULL;
}
}
-static AST* expect_lit(Parser* p, TokType type)
-{
- Tok* tok = peek(p);
- expect(p, type);
- return token_to_tree(tok);
-}
-
return NULL;
}
-static AST* definition(Parser* p)
-{
- ////size_t mrk = mark(p);
- //expect(p,T_ID);
- //if (peek(p)->type == T_LPAR) {
- // //insert(p, T_ID, lexer_dup("fn"));
- // fn_stmnt(p);
- //} else {
- // expression(p);
- // expect(p,T_END);
- //}
- ////reduce(p, mrk);
- return NULL;
-}
-
static AST* arglist(Parser* p)
{
////size_t mrk = mark(p);
static Tok* TextTok(TokType type, char* text)
{
Tok* tok = Token(type);
- tok->value.text = dupstring(text);
+ tok->value.text = (char*)gc_addref(dupstring(text));
return tok;
}
tok = punctuation(text);
} else if ('"' == text[0]) {
text[strlen(text)-1] = '\0';
- tok = TextTok(T_STRING, dupstring(&text[1]));
+ tok = TextTok(T_STRING, &text[1]);
} else if (text[0] == '\\') {
tok = character(text);
} else if ((text[0] == '0') && char_oneof("bodh",text[1])) {
Tok* tok = NULL;
size_t line, col;
char* text = scan(ctx, &line, &col);
- if (text != NULL)
+ if (text != NULL) {
tok = classify(NULL, line, col, text);
+ free(text);
+ }
return tok;
}
exit(1);
}
-bool accept(Parser* parser, TokType type)
+Tok* accept(Parser* parser, TokType type)
{
- bool ret = false;
- if (peek(parser)->type == type) {
+ Tok* tok = peek(parser);
+ if (tok->type == type) {
gc_swapref((void**)&(parser->tok), NULL);
- ret = true;
+ return tok;
}
- return ret;
+ return NULL;
}
-bool accept_str(Parser* parser, TokType type, const char* text)
+Tok* accept_str(Parser* parser, TokType type, const char* text)
{
- bool ret = false;
- if ((peek(parser)->type == type) && (0 == strcmp((char*)(parser->tok->value.text), text))) {
+ Tok* tok = peek(parser);
+ if ((tok->type == type) && (0 == strcmp((char*)(tok->value.text), text))) {
gc_swapref((void**)&(parser->tok), NULL);
- ret = true;
+ return tok;
}
- return ret;
+ return NULL;
}
-bool expect(Parser* parser, TokType type)
+Tok* expect(Parser* parser, TokType type)
{
- bool ret = false;
- if (accept(parser, type)) {
- ret = true;
- } else {
+ Tok* tok = accept(parser, type);
+ if (tok == NULL) {
error(parser, "Unexpected token");
}
- return ret;
+ return tok;
}
-bool expect_str(Parser* parser, TokType type, const char* text)
+Tok* expect_str(Parser* parser, TokType type, const char* text)
{
- bool ret = false;
- if (accept_str(parser, type, text)) {
- ret = true;
- } else {
+ Tok* tok = accept_str(parser, type, text);
+ if (tok == NULL) {
error(parser, "Unexpected token");
}
- return ret;
+ return tok;
}
pprint_literal(file, tree, depth);
} else if (tree->type == AST_REQ) {
printf("(require \"%s\")", require_name(tree));
+ } else if (tree->type == AST_DEF) {
+ printf("(def %s ", def_name(tree));
+ pprint_tree(file, def_value(tree), depth);
+ printf(")");
} else {
//fputs("(tree", file);
//vec_t* p_vec = tree->ptr.vec;
} AST;
/* String */
-AST* String(char* val);
+AST* String(Tok* val);
char* string_value(AST* val);
/* Symbol */
-AST* Symbol(char* val);
+AST* Symbol(Tok* val);
char* symbol_value(AST* val);
/* Character */
-AST* Char(uint32_t val);
+AST* Char(Tok* val);
uint32_t char_value(AST* val);
/* Integer */
-AST* Integer(intptr_t val);
+AST* Integer(Tok* val);
intptr_t integer_value(AST* val);
/* Float */
-AST* Float(double val);
+AST* Float(Tok* val);
double float_value(AST* val);
/* Bool */
-AST* Bool(bool val);
+AST* Bool(Tok* val);
bool bool_value(AST* val);
/* Ident */
-AST* Ident(char* val);
+AST* Ident(Tok* val);
char* ident_value(AST* val);
/* Require */
-AST* Require(char* name);
+AST* Require(Tok* name);
char* require_name(AST* req);
+/* Definition */
+AST* Def(Tok* name, AST* value);
+char* def_name(AST* def);
+AST* def_value(AST* def);
+
-//
-///* Definition */
-//AST* Def(char* name, AST* value);
-//char* def_name(AST* def);
-//AST* def_value(AST* def);
-//
///* Annotation */
//AST* Ann(char* name, AST* value);
//char* ann_name(AST* def);
bool parser_eof(Parser* p_parser);
void parser_resume(Parser* p_parser);
void error(Parser* p_parser, const char* p_text);
-bool accept(Parser* p_parser, TokType type);
-bool accept_str(Parser* p_parser, TokType type, const char* p_text);
-bool expect(Parser* p_parser, TokType type);
-bool expect_str(Parser* p_parser, TokType type, const char* p_text);
+Tok* accept(Parser* p_parser, TokType type);
+Tok* accept_str(Parser* p_parser, TokType type, const char* p_text);
+Tok* expect(Parser* p_parser, TokType type);
+Tok* expect_str(Parser* p_parser, TokType type, const char* p_text);
// Grammar Routines
AST* toplevel(Parser* p);