more tests for the runtime

This commit is contained in:
Francesco Cozzuto
2023-01-23 01:06:29 +01:00
parent 1780826a97
commit f7a2cb0611
48 changed files with 597 additions and 14 deletions
+2
View File
@@ -0,0 +1,2 @@
- talk about how functions are variables in the docs
- tests for MUL and DIV bytecodes
+1
View File
@@ -0,0 +1 @@
1 + 2 , 3 * 4 * 5;
+35 -7
View File
@@ -189,7 +189,16 @@ static bool parseIntegerOperand(Context *ctx, Error *error, Operand *op)
// pointing to a sequence of one or more digits // pointing to a sequence of one or more digits
// NOT followed by a dot and a digit after that // NOT followed by a dot and a digit after that
// (so this is an integer for sure, not a float). // (so this is an integer for sure, not a float).
assert(ctx->cur < ctx->len && isdigit(ctx->str[ctx->cur])); // Also, the sequence may be preceded by a '+' or
// '-'.
int sign = 1;
if (ctx->str[ctx->cur] == '+')
ctx->cur++;
else if (ctx->str[ctx->cur] == '-') {
sign = -1;
ctx->cur++;
}
long long int buffer = 0; long long int buffer = 0;
do { do {
@@ -209,9 +218,9 @@ static bool parseIntegerOperand(Context *ctx, Error *error, Operand *op)
} while(ctx->cur < ctx->len && isdigit(ctx->str[ctx->cur])); } while(ctx->cur < ctx->len && isdigit(ctx->str[ctx->cur]));
// Not a float! // Not a float!
op->type = OPTP_INT; op->type = OPTP_INT;
op->as_int = buffer; op->as_int = sign * buffer;
return true; return true;
} }
@@ -224,7 +233,16 @@ static bool parseFloatingOperand(Context *ctx, Error *error, Operand *op)
// It's ensured by the caller that the cursor is // It's ensured by the caller that the cursor is
// pointing to a sequence of one or more digits // pointing to a sequence of one or more digits
// followed by a dot and a digit after that. // followed by a dot and a digit after that.
assert(ctx->cur < ctx->len && isdigit(ctx->str[ctx->cur])); // Also, the sequence may be preceded by a '+' or
// '-'.
int sign = 1;
if (ctx->str[ctx->cur] == '+')
ctx->cur++;
else if (ctx->str[ctx->cur] == '-') {
sign = -1;
ctx->cur++;
}
double buffer = 0; double buffer = 0;
do { do {
@@ -256,7 +274,7 @@ static bool parseFloatingOperand(Context *ctx, Error *error, Operand *op)
} while(ctx->cur < ctx->len && isdigit(ctx->str[ctx->cur])); } while(ctx->cur < ctx->len && isdigit(ctx->str[ctx->cur]));
op->type = OPTP_FLOAT; op->type = OPTP_FLOAT;
op->as_float = buffer; op->as_float = sign * buffer;
return true; return true;
} }
@@ -277,11 +295,21 @@ static bool parseOperands(Context *ctx, BPAlloc *alloc, Error *error,
if(!parseStringOperand(ctx, error, alloc, &op)) if(!parseStringOperand(ctx, error, alloc, &op))
return false; return false;
} else if(isdigit(c)) { } else if(isdigit(c) || c == '-' || c == '+') {
/* Integer or float operand */ /* Integer or float operand */
size_t k = ctx->cur; size_t k = ctx->cur;
if (c == '-' || c == '+') {
k++;
if (k == ctx->len || !isdigit(ctx->str[k])) {
*ctx->error_offset = k;
Error_Report(error, ErrorType_SYNTAX, "Invalid character '%c' not followed by a digit", c);
return false;
}
}
while(k < ctx->len && isdigit(ctx->str[k])) while(k < ctx->len && isdigit(ctx->str[k]))
k += 1; k += 1;
+2 -1
View File
@@ -1758,7 +1758,8 @@ static Node *parse_expression_2(Context *ctx, Node *left_expr, int min_prec, _Bo
while(isbinop(ctx->token) && (precedenceof(ctx->token) > precedenceof(op) || (precedenceof(ctx->token) == precedenceof(op) && isrightassoc(ctx->token)))) while(isbinop(ctx->token) && (precedenceof(ctx->token) > precedenceof(op) || (precedenceof(ctx->token) == precedenceof(op) && isrightassoc(ctx->token))))
{ {
right_expr = parse_expression_2(ctx, right_expr, precedenceof(op) + 1, allow_toplev_tuples, allow_assignments); int new_prec = precedenceof(op) + (precedenceof(op) < precedenceof(ctx->token));
right_expr = parse_expression_2(ctx, right_expr, new_prec, allow_toplev_tuples, allow_assignments);
if(right_expr == NULL) if(right_expr == NULL)
return NULL; return NULL;
+1 -1
View File
@@ -884,7 +884,7 @@ static _Bool runInstruction(Runtime *runtime, Error *error)
return Runtime_Push(runtime, error, obj); return Runtime_Push(runtime, error, obj);
} }
case OPCODE_PUSHFLT: case OPCODE_PUSHFLT:
{ {
ASSERT(opc == 1); ASSERT(opc == 1);
-4
View File
@@ -114,10 +114,6 @@ ErrorID findField(NojaTestScanner *scanner)
return ErrorID_VOID; return ErrorID_VOID;
} }
// Returns:
// 0 if there are no more fields
// -1 on error
// 1 if a field was parsed
ErrorID parseField(NojaTestScanner *scanner, NojaTestField *field) ErrorID parseField(NojaTestScanner *scanner, NojaTestField *field)
{ {
ErrorID error; ErrorID error;
@@ -1,5 +1,5 @@
@type [compiler] @type [compiler]
@source @source
fun nop(a = 1) fun nop(a = 1)
@@ -1,5 +1,6 @@
@type [compiler] @type [compiler]
@source @source
if true: if true:
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT 90.22;
PUSHFLT 700.39;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [790.61]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT -90.22;
PUSHFLT 700.39;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [610.17]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT 90.22;
PUSHFLT -700.39;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-610.17]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT -90.22;
PUSHFLT -700.39;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-790.61]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT 90.39;
PUSHINT 700;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [790.39]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT -90.39;
PUSHINT 700;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [609.61]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT 90.39;
PUSHINT -700;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-609.61]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT -90.39;
PUSHINT -700;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-790.39]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT 90;
PUSHFLT 700.39;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [790.39]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT 90;
PUSHFLT -700.39;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-610.39]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT -90;
PUSHFLT 700.39;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [610.39]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT -90;
PUSHFLT -700.39;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-790.39]
+14
View File
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT 90;
PUSHINT 700;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [790]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT -90;
PUSHINT 700;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [610]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT 90;
PUSHINT -700;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-610]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT -90;
PUSHINT -700;
ADD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-790]
+14
View File
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT 100;
PUSHINT 8;
MOD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [4]
+14
View File
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT 8;
PUSHINT 100;
MOD;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [8]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT 90.22;
PUSHFLT 700.39;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-610.17]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT -90.22;
PUSHFLT 700.39;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-790.61]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT 90.22;
PUSHFLT -700.39;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [790.61]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT -90.22;
PUSHFLT -700.39;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [610.17]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT 90.39;
PUSHINT 700;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-609.61]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT -90.39;
PUSHINT 700;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-790.39]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT 90.39;
PUSHINT -700;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [790.39]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHFLT -90.39;
PUSHINT -700;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [609.61]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT 90;
PUSHFLT 700.39;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-610.39]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT 90;
PUSHFLT -700.39;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [790.39]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT -90;
PUSHFLT 700.39;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-790.39]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT -90;
PUSHFLT -700.39;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [610.39]
+14
View File
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT 90;
PUSHINT 700;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-610]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT -90;
PUSHINT 700;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-790]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT 90;
PUSHINT -700;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [790]
@@ -0,0 +1,14 @@
@type [runtime]
@bytecode
PUSHINT -90;
PUSHINT -700;
SUB;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [610]
+13
View File
@@ -0,0 +1,13 @@
@type [runtime]
@bytecode
PUSHINT 23;
NEG;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-23]
+13
View File
@@ -0,0 +1,13 @@
@type [runtime]
@bytecode
PUSHINT -23;
NEG;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [23]
+13
View File
@@ -0,0 +1,13 @@
@type [runtime]
@bytecode
PUSHFLT 2.3;
NEG;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [-2.30]
+13
View File
@@ -0,0 +1,13 @@
@type [runtime]
@bytecode
PUSHFLT -2.3;
NEG;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [2.30]
+13
View File
@@ -0,0 +1,13 @@
@type [runtime]
@bytecode
PUSHTRU;
NOT;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [false]
+13
View File
@@ -0,0 +1,13 @@
@type [runtime]
@bytecode
PUSHFLS;
NOT;
PUSHVAR "print";
CALL 1, 1;
POP 1;
EXIT;
@output [true]