Compare commits
2 Commits
298883939b
...
ef63742015
Author | SHA1 | Date | |
---|---|---|---|
|
ef63742015 | ||
|
7ae9ddaee9 |
@ -5,7 +5,11 @@ iterations := 100
|
|||||||
|
|
||||||
while k - iterations {
|
while k - iterations {
|
||||||
k_f := float(k)
|
k_f := float(k)
|
||||||
sum = sum + 1.0 / pow(16.0, k_f) * (4.0 / (8.0 * k_f + 1.0) - 2.0 / (8.0 * k_f + 4.0) - 1.0 / (8.0 * k_f + 5.0) - 1.0 / (8.0 * k_f + 6.0))
|
sum = sum + 1.0 / pow(16.0, k_f) *
|
||||||
|
(4.0 / (8.0 * k_f + 1.0) -
|
||||||
|
2.0 / (8.0 * k_f + 4.0) -
|
||||||
|
1.0 / (8.0 * k_f + 5.0) -
|
||||||
|
1.0 / (8.0 * k_f + 6.0))
|
||||||
k = k + 1
|
k = k + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
8
parse.c
8
parse.c
@ -177,6 +177,14 @@ static ExprRet expr(IRToks *out_ir, TokList *toks, Map *funcs, Scope *parent_sc,
|
|||||||
negate = true;
|
negate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Delete newline if we're definitely expecting an operand. */
|
||||||
|
if (t->tok.kind == TokOp && t->tok.Op == OpNewLn) {
|
||||||
|
if (t == start)
|
||||||
|
start = t->next;
|
||||||
|
t = t->next;
|
||||||
|
toklist_del(toks, t->prev, t->prev);
|
||||||
|
}
|
||||||
|
|
||||||
/* Collapse parentheses. */
|
/* Collapse parentheses. */
|
||||||
if (t->tok.kind == TokOp && t->tok.Op == OpLParen) {
|
if (t->tok.kind == TokOp && t->tok.Op == OpLParen) {
|
||||||
ExprRet r;
|
ExprRet r;
|
||||||
|
26
vm.c
26
vm.c
@ -43,6 +43,10 @@ static Value *irparam_to_val(Stack *s, IRParam *v) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void run(const IRToks *ir, const BuiltinFunc *builtin_funcs) {
|
void run(const IRToks *ir, const BuiltinFunc *builtin_funcs) {
|
||||||
|
/* so we don't have to call malloc on every function call */
|
||||||
|
size_t fn_args_cap = 16;
|
||||||
|
Value *fn_args = xmalloc(sizeof(Value) * fn_args_cap);
|
||||||
|
|
||||||
Stack s = stack_make();
|
Stack s = stack_make();
|
||||||
for (size_t i = 0; i < ir->len;) {
|
for (size_t i = 0; i < ir->len;) {
|
||||||
IRTok *instr = &ir->toks[i];
|
IRTok *instr = &ir->toks[i];
|
||||||
@ -52,17 +56,18 @@ void run(const IRToks *ir, const BuiltinFunc *builtin_funcs) {
|
|||||||
case IRSet:
|
case IRSet:
|
||||||
case IRNeg:
|
case IRNeg:
|
||||||
stack_fit(&s, instr->Unary.addr);
|
stack_fit(&s, instr->Unary.addr);
|
||||||
TRY(s.mem[instr->Unary.addr] = eval_unary(instr->instr, irparam_to_val(&s, &instr->Unary.val)));
|
TRY_ELSE(s.mem[instr->Unary.addr] = eval_unary(instr->instr, irparam_to_val(&s, &instr->Unary.val)),
|
||||||
|
{free(fn_args); stack_term(&s);});
|
||||||
break;
|
break;
|
||||||
case IRAdd:
|
case IRAdd:
|
||||||
case IRSub:
|
case IRSub:
|
||||||
case IRDiv:
|
case IRDiv:
|
||||||
case IRMul:
|
case IRMul:
|
||||||
stack_fit(&s, instr->Arith.addr);
|
stack_fit(&s, instr->Arith.addr);
|
||||||
TRY(s.mem[instr->Arith.addr] = eval_arith(instr->instr,
|
TRY_ELSE(s.mem[instr->Arith.addr] = eval_arith(instr->instr,
|
||||||
irparam_to_val(&s, &instr->Arith.lhs),
|
irparam_to_val(&s, &instr->Arith.lhs),
|
||||||
irparam_to_val(&s, &instr->Arith.rhs)
|
irparam_to_val(&s, &instr->Arith.rhs)),
|
||||||
));
|
{free(fn_args); stack_term(&s);});
|
||||||
break;
|
break;
|
||||||
case IRJmp:
|
case IRJmp:
|
||||||
i = instr->Jmp.iaddr;
|
i = instr->Jmp.iaddr;
|
||||||
@ -75,14 +80,16 @@ void run(const IRToks *ir, const BuiltinFunc *builtin_funcs) {
|
|||||||
break;
|
break;
|
||||||
case IRCallInternal: {
|
case IRCallInternal: {
|
||||||
const BuiltinFunc *f = &builtin_funcs[instr->CallI.fid];
|
const BuiltinFunc *f = &builtin_funcs[instr->CallI.fid];
|
||||||
Value *args = xmalloc(sizeof(Value) * f->n_args);
|
/* make sure enough space for our arguments is allocated */
|
||||||
|
if (f->n_args > fn_args_cap)
|
||||||
|
fn_args = xrealloc(fn_args, sizeof(Value) * (fn_args_cap = f->n_args));
|
||||||
|
/* copy arguments into buffer */
|
||||||
for (size_t i = 0; i < f->n_args; i++)
|
for (size_t i = 0; i < f->n_args; i++)
|
||||||
args[i] = *irparam_to_val(&s, &instr->CallI.args[i]);
|
fn_args[i] = *irparam_to_val(&s, &instr->CallI.args[i]);
|
||||||
|
|
||||||
stack_fit(&s, instr->CallI.ret_addr);
|
stack_fit(&s, instr->CallI.ret_addr);
|
||||||
TRY_ELSE(s.mem[instr->CallI.ret_addr] = f->func(args), free(args));
|
TRY_ELSE(s.mem[instr->CallI.ret_addr] = f->func(fn_args),
|
||||||
|
{free(fn_args); stack_term(&s);});
|
||||||
free(args);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@ -92,4 +99,5 @@ void run(const IRToks *ir, const BuiltinFunc *builtin_funcs) {
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
stack_term(&s);
|
stack_term(&s);
|
||||||
|
free(fn_args);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user