map can have function members by placing function declaration between the curly braces
This commit is contained in:
@@ -56,7 +56,7 @@ fun stringFromInteger(n: int) {
|
||||
|
||||
temp = abs(n); # Temporary copy of the input
|
||||
text = ""; # The output we're about to compute
|
||||
|
||||
|
||||
while power >= 1: {
|
||||
# Pop the leftmost digit from [temp].
|
||||
digit = temp / power; # Get the leftmost digit
|
||||
@@ -78,6 +78,7 @@ fun floatFromInteger(n: int)
|
||||
return 1.0 * n;
|
||||
|
||||
fun integerFromDigit(char: String) {
|
||||
ord = string.ord;
|
||||
res = ord(char) - ord('0');
|
||||
if res < 0 or res > 9:
|
||||
error("String isn't a digit");
|
||||
|
||||
+13
-6
@@ -70,6 +70,7 @@ typedef enum {
|
||||
EXPR_INT,
|
||||
EXPR_MAP,
|
||||
EXPR_CALL,
|
||||
EXPR_FUNC,
|
||||
EXPR_PAIR,
|
||||
EXPR_LIST,
|
||||
EXPR_NONE,
|
||||
@@ -157,6 +158,14 @@ typedef struct {
|
||||
Node *set;
|
||||
} IndexSelectionExprNode;
|
||||
|
||||
#warning "temp"
|
||||
typedef struct {
|
||||
ExprNode base;
|
||||
Node *argv;
|
||||
int argc;
|
||||
Node *body;
|
||||
} FuncExprNode;
|
||||
|
||||
typedef struct {
|
||||
Node base;
|
||||
Node *condition;
|
||||
@@ -187,12 +196,10 @@ typedef struct {
|
||||
} ReturnNode;
|
||||
|
||||
typedef struct {
|
||||
Node base;
|
||||
char *name;
|
||||
Node *argv;
|
||||
int argc;
|
||||
Node *body;
|
||||
} FunctionNode;
|
||||
Node base;
|
||||
StringExprNode *name;
|
||||
FuncExprNode *expr;
|
||||
} FuncDeclNode;
|
||||
|
||||
typedef struct {
|
||||
Node base;
|
||||
|
||||
@@ -255,7 +255,7 @@ static void emitInstrForArgumentNode(CodegenContext *ctx, ArgumentNode *arg, int
|
||||
emitInstr_POP1(ctx, arg->base.offset, arg->base.length);
|
||||
}
|
||||
|
||||
static void emitInstrForFuncNode(CodegenContext *ctx, FunctionNode *func)
|
||||
static void emitInstrForFuncExprNode(CodegenContext *ctx, FuncExprNode *func)
|
||||
{
|
||||
Label *label_func = Label_New(ctx);
|
||||
Label *label_jump = Label_New(ctx);
|
||||
@@ -266,11 +266,9 @@ static void emitInstrForFuncNode(CodegenContext *ctx, FunctionNode *func)
|
||||
{ .type = OPTP_PROMISE, .as_promise = Label_ToPromise(label_func) },
|
||||
{ .type = OPTP_INT, .as_int = func->argc },
|
||||
};
|
||||
CodegenContext_EmitInstr(ctx, OPCODE_PUSHFUN, ops, 2, func->base.offset, func->base.length);
|
||||
CodegenContext_EmitInstr(ctx, OPCODE_PUSHFUN, ops, 2, func->base.base.offset, func->base.base.length);
|
||||
}
|
||||
emitInstr_ASS(ctx, func->name, func->base.offset, func->base.length); // Assign variable
|
||||
emitInstr_POP1(ctx, func->base.offset, func->base.length); // Pop function object
|
||||
emitInstr_JUMP(ctx, label_jump, func->base.offset, func->base.length); // Jump after the function code
|
||||
emitInstr_JUMP(ctx, label_jump, func->base.base.offset, func->base.base.length); // Jump after the function code
|
||||
Label_SetHere(label_func, ctx); // This is the function code index.
|
||||
|
||||
// Compile the function body.
|
||||
@@ -302,6 +300,12 @@ static void emitInstrForFuncNode(CodegenContext *ctx, FunctionNode *func)
|
||||
Label_Free(label_jump);
|
||||
}
|
||||
|
||||
static void emitInstrForFuncDeclNode(CodegenContext *ctx, FuncDeclNode *func)
|
||||
{
|
||||
emitInstrForFuncExprNode(ctx, func->expr);
|
||||
emitInstr_ASS(ctx, func->name->val, func->base.offset, func->base.length); // Assign variable
|
||||
emitInstr_POP1(ctx, func->base.offset, func->base.length); // Pop function object
|
||||
}
|
||||
|
||||
static void flattenTupleTree(CodegenContext *ctx, ExprNode *root, ExprNode *tuple[], int max, int *count)
|
||||
{
|
||||
@@ -569,6 +573,10 @@ static void emitInstrForExprNode(CodegenContext *ctx, ExprNode *expr,
|
||||
emitInstrForFuncCallNode(ctx, (CallExprNode*) expr, label_break, 1);
|
||||
return;
|
||||
|
||||
case EXPR_FUNC:
|
||||
emitInstrForFuncExprNode(ctx, (FuncExprNode*) expr);
|
||||
return;
|
||||
|
||||
case EXPR_SELECT:
|
||||
{
|
||||
IndexSelectionExprNode *sel = (IndexSelectionExprNode*) expr;
|
||||
@@ -723,7 +731,7 @@ static void emitInstrForNode(CodegenContext *ctx, Node *node, Label *label_break
|
||||
}
|
||||
|
||||
case NODE_FUNC:
|
||||
emitInstrForFuncNode(ctx, (FunctionNode*) node);
|
||||
emitInstrForFuncDeclNode(ctx, (FuncDeclNode*) node);
|
||||
return;
|
||||
|
||||
default:
|
||||
|
||||
+115
-73
@@ -136,7 +136,7 @@ static Node *parse_expression(Context *ctx, _Bool allow_toplev_tuples, _Bool all
|
||||
static Node *parse_expression_statement(Context *ctx);
|
||||
static Node *parse_ifelse_statement(Context *ctx);
|
||||
static Node *parse_compound_statement(Context *ctx, TokenKind end);
|
||||
static Node *parse_function_definition(Context *ctx);
|
||||
static FuncDeclNode *parse_function_definition(Context *ctx);
|
||||
static Node *parse_postfix_expression(Context *ctx);
|
||||
static Node *parse_prefix_expression(Context *ctx);
|
||||
static Node *parse_while_statement(Context *ctx);
|
||||
@@ -656,7 +656,7 @@ static Node *parse_statement(Context *ctx)
|
||||
}
|
||||
|
||||
case TKWFUN:
|
||||
return parse_function_definition(ctx);
|
||||
return (Node*) parse_function_definition(ctx);
|
||||
|
||||
case TKWWHILE:
|
||||
return parse_while_statement(ctx);
|
||||
@@ -965,58 +965,69 @@ static Node *parse_map_primary_expression(Context *ctx)
|
||||
while(1)
|
||||
{
|
||||
Node *key;
|
||||
_Bool key_is_a_single_ident;
|
||||
Node *item;
|
||||
|
||||
bool item_is_func_decl = false;
|
||||
|
||||
if (current(ctx) == TKWFUN)
|
||||
{
|
||||
_Bool key_starts_with_an_ident = current(ctx) == TIDENT;
|
||||
|
||||
next(ctx);
|
||||
|
||||
key_is_a_single_ident = key_starts_with_an_ident && (current(ctx) == TDONE || current(ctx) == ':');
|
||||
|
||||
prev(ctx);
|
||||
}
|
||||
|
||||
if(key_is_a_single_ident)
|
||||
{
|
||||
assert(current(ctx) == TIDENT);
|
||||
key = makeStringExprNode(ctx, ctx->src + ctx->token->offset, ctx->token->length);
|
||||
|
||||
next(ctx);
|
||||
FuncDeclNode *func = parse_function_definition(ctx);
|
||||
if (func == NULL) return NULL;
|
||||
key = (Node*) func->name;
|
||||
item = (Node*) func->expr;
|
||||
item_is_func_decl = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
key = parse_expression(ctx, 0, 1);
|
||||
_Bool key_is_a_single_ident;
|
||||
{
|
||||
_Bool key_starts_with_an_ident = current(ctx) == TIDENT;
|
||||
|
||||
next(ctx);
|
||||
|
||||
key_is_a_single_ident = key_starts_with_an_ident && (current(ctx) == TDONE || current(ctx) == ':');
|
||||
|
||||
prev(ctx);
|
||||
}
|
||||
|
||||
if(key_is_a_single_ident)
|
||||
{
|
||||
assert(current(ctx) == TIDENT);
|
||||
key = makeStringExprNode(ctx, ctx->src + ctx->token->offset, ctx->token->length);
|
||||
|
||||
next(ctx);
|
||||
}
|
||||
else
|
||||
key = parse_expression(ctx, 0, 1);
|
||||
|
||||
if(key == NULL)
|
||||
return NULL;
|
||||
|
||||
if(done(ctx))
|
||||
{
|
||||
Error_Report(ctx->error, 0,
|
||||
"Source ended where a map key-value "
|
||||
"separator ':' was expected");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(current(ctx) != ':')
|
||||
{
|
||||
Error_Report(ctx->error, 0,
|
||||
"Got token \"%.*s\" where a map key-value "
|
||||
"separator ':' was expected",
|
||||
ctx->token->length,
|
||||
ctx->src + ctx->token->offset);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
next(ctx);
|
||||
|
||||
// Parse.
|
||||
item = parse_expression(ctx, 0, 1);
|
||||
if(item == NULL) return NULL;
|
||||
}
|
||||
|
||||
if(key == NULL)
|
||||
return NULL;
|
||||
|
||||
if(done(ctx))
|
||||
{
|
||||
Error_Report(ctx->error, 0,
|
||||
"Source ended where a map key-value "
|
||||
"separator ':' was expected");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(current(ctx) != ':')
|
||||
{
|
||||
Error_Report(ctx->error, 0,
|
||||
"Got token \"%.*s\" where a map key-value "
|
||||
"separator ':' was expected",
|
||||
ctx->token->length,
|
||||
ctx->src + ctx->token->offset);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
next(ctx);
|
||||
|
||||
// Parse.
|
||||
Node *item = parse_expression(ctx, 0, 1);
|
||||
|
||||
if(item == NULL)
|
||||
return NULL;
|
||||
|
||||
// Append.
|
||||
if(tail)
|
||||
{
|
||||
@@ -1038,19 +1049,20 @@ static Node *parse_map_primary_expression(Context *ctx)
|
||||
if(current(ctx) == '}')
|
||||
break;
|
||||
|
||||
if(current(ctx) != ',')
|
||||
{
|
||||
if(current(ctx) == TDONE)
|
||||
Error_Report(ctx->error, 0, "Source ended inside a map literal");
|
||||
else
|
||||
Error_Report(ctx->error, 0,
|
||||
"Got unexpected token \"%.*s\" inside "
|
||||
"map literal, where ',' or '}' were expected",
|
||||
ctx->token->length, ctx->src + ctx->token->offset);
|
||||
return NULL;
|
||||
if (!item_is_func_decl) {
|
||||
if(current(ctx) != ',')
|
||||
{
|
||||
if(current(ctx) == TDONE)
|
||||
Error_Report(ctx->error, 0, "Source ended inside a map literal");
|
||||
else
|
||||
Error_Report(ctx->error, 0,
|
||||
"Got unexpected token \"%.*s\" inside "
|
||||
"map literal, where ',' or '}' were expected",
|
||||
ctx->token->length, ctx->src + ctx->token->offset);
|
||||
return NULL;
|
||||
}
|
||||
next(ctx); // Skip the ','.
|
||||
}
|
||||
|
||||
next(ctx); // Skip the ','.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2001,7 +2013,7 @@ static _Bool parse_function_arguments(Context *ctx, int *argc_, Node **argv_)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static Node *parse_function_definition(Context *ctx)
|
||||
static FuncDeclNode *parse_function_definition(Context *ctx)
|
||||
{
|
||||
assert(ctx != NULL);
|
||||
|
||||
@@ -2028,13 +2040,15 @@ static Node *parse_function_definition(Context *ctx)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *name = copy_token_text(ctx);
|
||||
|
||||
if(name == NULL)
|
||||
char *name_val = copy_token_text(ctx);
|
||||
if(name_val == NULL)
|
||||
{
|
||||
Error_Report(ctx->error, 1, "No memory");
|
||||
return NULL;
|
||||
}
|
||||
int name_len = strlen(name_val);
|
||||
int name_offset = current_token(ctx)->offset;
|
||||
int name_length = current_token(ctx)->length;
|
||||
|
||||
int argc = 0; // Initialization for the warning.
|
||||
Node *argv;
|
||||
@@ -2052,28 +2066,56 @@ static Node *parse_function_definition(Context *ctx)
|
||||
if(body == NULL)
|
||||
return NULL;
|
||||
|
||||
FunctionNode *func;
|
||||
FuncDeclNode *func;
|
||||
{
|
||||
// Make argument node.
|
||||
func = BPAlloc_Malloc(ctx->alloc, sizeof(FunctionNode));
|
||||
int length = body->offset
|
||||
+ body->length
|
||||
- offset;
|
||||
|
||||
FuncExprNode *expr = BPAlloc_Malloc(ctx->alloc, sizeof(FuncExprNode));
|
||||
if (expr == NULL)
|
||||
{
|
||||
Error_Report(ctx->error, 1, "No memory");
|
||||
return NULL;
|
||||
}
|
||||
expr->base.base.kind = NODE_EXPR;
|
||||
expr->base.base.next = NULL;
|
||||
expr->base.base.offset = offset;
|
||||
expr->base.base.length = length;
|
||||
expr->base.kind = EXPR_FUNC;
|
||||
expr->argv = argv;
|
||||
expr->argc = argc;
|
||||
expr->body = body;
|
||||
|
||||
StringExprNode *name = BPAlloc_Malloc(ctx->alloc, sizeof(StringExprNode));
|
||||
if (name == NULL)
|
||||
{
|
||||
Error_Report(ctx->error, 1, "No memory");
|
||||
return NULL;
|
||||
}
|
||||
name->base.base.kind = NODE_EXPR;
|
||||
name->base.base.next = NULL;
|
||||
name->base.base.offset = name_offset;
|
||||
name->base.base.length = name_length;
|
||||
name->base.kind = EXPR_STRING;
|
||||
name->val = name_val;
|
||||
name->len = name_len;
|
||||
|
||||
func = BPAlloc_Malloc(ctx->alloc, sizeof(FuncDeclNode));
|
||||
if(func == NULL)
|
||||
{
|
||||
Error_Report(ctx->error, 1, "No memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
func->base.kind = NODE_FUNC;
|
||||
func->base.next = NULL;
|
||||
func->base.offset = offset;
|
||||
func->base.length = body->offset + body->length - offset;
|
||||
func->base.length = length;
|
||||
func->name = name;
|
||||
func->argv = argv;
|
||||
func->argc = argc;
|
||||
func->body = body;
|
||||
func->expr = expr;
|
||||
}
|
||||
|
||||
return (Node*) func;
|
||||
return func;
|
||||
}
|
||||
|
||||
static Node *parse_while_statement(Context *ctx)
|
||||
|
||||
Reference in New Issue
Block a user