Compare commits
16 Commits
math-branc
...
925077d55e
| Author | SHA1 | Date | |
|---|---|---|---|
| 925077d55e | |||
| 32d6a029dd | |||
| dac983b684 | |||
| d3c03b4987 | |||
| 0f155c80be | |||
| 4680597065 | |||
| c6762a7966 | |||
| 792aed13ae | |||
| 08b1edd7a7 | |||
| 8eef78c310 | |||
| ec23d55f9d | |||
| 724162f42e | |||
| 6d0dd99406 | |||
| 7717a40574 | |||
| bbf2277b6f | |||
| fd6fbbaed5 |
@@ -14,17 +14,6 @@ extern "C" {
|
||||
struct GroundScope;
|
||||
typedef struct GroundScope GroundScope;
|
||||
|
||||
/*
|
||||
* Stores data associated with an error thrown during Ground execution.
|
||||
*/
|
||||
typedef struct GroundError {
|
||||
char* what;
|
||||
char* type;
|
||||
struct GroundInstruction* where;
|
||||
size_t line;
|
||||
bool hasLine;
|
||||
} GroundError;
|
||||
|
||||
// Creates a GroundValue containing (in), with type ERROR.
|
||||
GroundValue createErrorGroundValue(GroundError in);
|
||||
|
||||
|
||||
@@ -11,13 +11,14 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <uthash.h>
|
||||
|
||||
typedef enum GroundInstType {
|
||||
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE, DROP
|
||||
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD
|
||||
} GroundInstType;
|
||||
|
||||
typedef enum GroundValueType {
|
||||
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, CUSTOM, NONE
|
||||
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, STRUCTVAL, CUSTOM, ERROR, NONE
|
||||
} GroundValueType;
|
||||
|
||||
typedef enum GroundArgType {
|
||||
@@ -41,6 +42,17 @@ typedef struct List {
|
||||
struct GroundValue* values;
|
||||
} List;
|
||||
|
||||
/*
|
||||
* Stores data associated with an error thrown during Ground execution.
|
||||
*/
|
||||
typedef struct GroundError {
|
||||
char* what;
|
||||
char* type;
|
||||
struct GroundInstruction* where;
|
||||
size_t line;
|
||||
bool hasLine;
|
||||
} GroundError;
|
||||
|
||||
/*
|
||||
* Stores literal values created in a Ground program.
|
||||
*/
|
||||
@@ -53,8 +65,10 @@ typedef struct GroundValue {
|
||||
char charVal;
|
||||
bool boolVal;
|
||||
List listVal;
|
||||
GroundError errorVal;
|
||||
struct GroundFunction* fnVal;
|
||||
void* customVal;
|
||||
struct GroundStruct* structVal;
|
||||
struct GroundObject* customVal;
|
||||
} data;
|
||||
} GroundValue;
|
||||
|
||||
@@ -115,6 +129,39 @@ typedef struct GroundFunction {
|
||||
size_t startLine;
|
||||
} GroundFunction;
|
||||
|
||||
/*
|
||||
* Field for a GroundStruct
|
||||
*/
|
||||
typedef struct GroundStructField {
|
||||
char id[64];
|
||||
GroundValue value;
|
||||
UT_hash_handle hh;
|
||||
} GroundStructField;
|
||||
|
||||
/*
|
||||
* Represents a Ground struct.
|
||||
*/
|
||||
typedef struct GroundStruct {
|
||||
GroundStructField* fields;
|
||||
size_t size;
|
||||
} GroundStruct;
|
||||
|
||||
/*
|
||||
* Field for a GroundObject
|
||||
*/
|
||||
typedef struct GroundObjectField {
|
||||
char id[64];
|
||||
GroundValue value;
|
||||
UT_hash_handle hh;
|
||||
} GroundObjectField;
|
||||
|
||||
/*
|
||||
* Represents an initialised Ground struct.
|
||||
*/
|
||||
typedef struct GroundObject {
|
||||
GroundObjectField* fields;
|
||||
} GroundObject;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -133,6 +180,9 @@ GroundValue groundCreateValue(GroundValueType type, ...);
|
||||
|
||||
GroundProgram groundParseFile(const char* code);
|
||||
|
||||
GroundStruct groundCreateStruct();
|
||||
void groundAddFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
152
libs/math/math.c
152
libs/math/math.c
@@ -1,5 +1,12 @@
|
||||
#include <groundext.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
#include <float.h>
|
||||
|
||||
|
||||
GroundValue ground_sin(GroundScope* scope, List args) {
|
||||
return groundCreateValue(
|
||||
@@ -21,19 +28,15 @@ GroundValue ground_tan(GroundScope* scope, List args) {
|
||||
}
|
||||
|
||||
GroundValue rad_to_deg(GroundScope* scope, List args) {
|
||||
double radians = args.values[0].data.doubleVal;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
radians * (180.0 / M_PI)
|
||||
args.values[0].data.doubleVal * (180.0 / M_PI)
|
||||
);
|
||||
}
|
||||
GroundValue deg_to_rad(GroundScope* scope, List args) {
|
||||
double deg = args.values[0].data.doubleVal;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
deg * (M_PI / 180.0)
|
||||
args.values[0].data.doubleVal * (M_PI / 180.0)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -62,16 +65,149 @@ GroundValue ground_sqrt(GroundScope* scope, List args) {
|
||||
);
|
||||
}
|
||||
|
||||
GroundValue ground_abs(GroundScope* scope, List args) {
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
abs(args.values[0].data.doubleVal)
|
||||
);
|
||||
}
|
||||
GroundValue ground_min(GroundScope* scope, List args) {
|
||||
double a = args.values[0].data.doubleVal;
|
||||
double b = args.values[1].data.doubleVal;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
(a < b) ? a : b
|
||||
);
|
||||
}
|
||||
GroundValue ground_max(GroundScope* scope, List args) {
|
||||
double a = args.values[0].data.doubleVal;
|
||||
double b = args.values[1].data.doubleVal;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
(a > b) ? a : b
|
||||
);
|
||||
}
|
||||
GroundValue ground_clamp(GroundScope* scope, List args) {
|
||||
double number = args.values[0].data.doubleVal;
|
||||
double min = args.values[1].data.doubleVal;
|
||||
double max = args.values[2].data.doubleVal;
|
||||
|
||||
if (number < min) number = min;
|
||||
if (number > max) number = max;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
number
|
||||
);
|
||||
}
|
||||
|
||||
GroundValue ground_round(GroundScope* scope, List args) {
|
||||
double x = args.values[0].data.doubleVal;
|
||||
|
||||
if (x >= 0.0)
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
(long long)(x + 0.5)
|
||||
);
|
||||
else
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
(long long)(x - 0.5)
|
||||
);
|
||||
}
|
||||
GroundValue ground_floor(GroundScope* scope, List args) {
|
||||
double x = args.values[0].data.doubleVal;
|
||||
|
||||
if (x >= (double)LLONG_MAX) return groundCreateValue(INT, LLONG_MAX);
|
||||
if (x <= (double)LLONG_MIN) return groundCreateValue(INT, LLONG_MIN);
|
||||
|
||||
long long i = (long long)x; // truncates toward zero
|
||||
|
||||
if (x < 0 && x != (double)i) {
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i-1
|
||||
);
|
||||
}
|
||||
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i
|
||||
);
|
||||
}
|
||||
GroundValue ground_ceil(GroundScope* scope, List args) {
|
||||
double x = args.values[0].data.doubleVal;
|
||||
|
||||
if (x >= (double)LLONG_MAX) return groundCreateValue(INT, LLONG_MAX);
|
||||
if (x <= (double)LLONG_MIN) return groundCreateValue(INT, LLONG_MIN);
|
||||
|
||||
|
||||
long long i = (long long)x; // truncates toward zero
|
||||
|
||||
if (x > 0 && x != (double)i) {
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i+1
|
||||
);
|
||||
}
|
||||
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i
|
||||
);
|
||||
}
|
||||
|
||||
GroundValue ground_random(GroundScope* scope, List args) {
|
||||
int64_t min = args.values[0].data.intVal;
|
||||
int64_t max = args.values[1].data.intVal;
|
||||
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
min + rand() % (max - min)
|
||||
);
|
||||
}
|
||||
GroundValue ground_random_double(GroundScope* scope, List args) {
|
||||
double min = args.values[0].data.doubleVal;
|
||||
double max = args.values[1].data.doubleVal;
|
||||
|
||||
return groundCreateValue(
|
||||
DOUBLE,
|
||||
min + (double)rand() / RAND_MAX * (max - min)
|
||||
);
|
||||
}
|
||||
GroundValue ground_random_set_seed(GroundScope* scope, List args) {
|
||||
srand(args.values[0].data.intVal);
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
void ground_init(GroundScope* scope) {
|
||||
srand((unsigned)time(NULL) ^ (unsigned)clock());
|
||||
|
||||
groundAddNativeFunction(scope, "math_Sin", ground_sin, DOUBLE, 1, DOUBLE, "radians");
|
||||
groundAddNativeFunction(scope, "math_Cos", ground_cos, DOUBLE, 1, DOUBLE, "radians");
|
||||
groundAddNativeFunction(scope, "math_Tan", ground_tan, DOUBLE, 1, DOUBLE, "radians");
|
||||
|
||||
groundAddNativeFunction(scope, "math_DegreesToRadians", deg_to_rad, DOUBLE, 1, DOUBLE, "degrees");
|
||||
groundAddNativeFunction(scope, "math_RadiansToDegrees", rad_to_deg, DOUBLE, 1, DOUBLE, "radians");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Modulos", ground_modulos, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
|
||||
groundAddNativeFunction(scope, "math_Pow", ground_pow, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Sqrt", ground_sqrt, DOUBLE, 1, DOUBLE, "number");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Abs", ground_abs, DOUBLE, 1, DOUBLE, "number");
|
||||
groundAddNativeFunction(scope, "math_Min", ground_min, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
|
||||
groundAddNativeFunction(scope, "math_Max", ground_max, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
|
||||
groundAddNativeFunction(scope, "math_Clamp", ground_clamp, DOUBLE, 2, DOUBLE, "number", DOUBLE, "min", DOUBLE, "max");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Round", ground_round, INT, 1, DOUBLE, "number");
|
||||
groundAddNativeFunction(scope, "math_Floor", ground_floor, INT, 1, DOUBLE, "number");
|
||||
groundAddNativeFunction(scope, "math_Ceil", ground_ceil, INT, 1, DOUBLE, "number");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Random", ground_random, INT, 2, INT, "min", INT, "max");
|
||||
groundAddNativeFunction(scope, "math_RandomDouble", ground_random_double, DOUBLE, 2, DOUBLE, "min", DOUBLE, "max");
|
||||
groundAddNativeFunction(scope, "math_RandomSetSeed", ground_random_set_seed, INT, 1, INT, "seed");
|
||||
}
|
||||
66
src/compiler.c
Normal file
66
src/compiler.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "compiler.h"
|
||||
#include "interpreter.h"
|
||||
#include "types.h"
|
||||
#include "include/estr.h"
|
||||
#include <stdint.h>
|
||||
|
||||
char* compileGroundProgram(GroundProgram* program) {
|
||||
Estr start = CREATE_ESTR("global _start\nsection .text\n_start:\n");
|
||||
Estr data = CREATE_ESTR("section .rodata\n");
|
||||
|
||||
// Create data section
|
||||
for (size_t i = 0; i < program->size; i++) {
|
||||
|
||||
}
|
||||
|
||||
// Generate assembly
|
||||
for (size_t i = 0; i < program->size; i++) {
|
||||
GroundInstruction gi = program->instructions[i];
|
||||
switch (gi.type) {
|
||||
case END: {
|
||||
if (gi.args.length < 1) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg for end instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.length > 1) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 1 arg for end instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].type != VALUE) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for end instruction", &gi, i);
|
||||
}
|
||||
if (gi.args.args[0].value.value.type != INT) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for end instruction", &gi, i);
|
||||
}
|
||||
|
||||
int64_t bigretint = gi.args.args[0].value.value.data.intVal;
|
||||
int retint;
|
||||
if (bigretint > 255) {
|
||||
retint = 255;
|
||||
}
|
||||
else if (bigretint < 0) {
|
||||
retint = 0;
|
||||
}
|
||||
else {
|
||||
retint = bigretint;
|
||||
}
|
||||
char retstr[8];
|
||||
snprintf(retstr, sizeof(retstr), "%d", retint);
|
||||
APPEND_ESTR(start, " mov rax, 60\n");
|
||||
APPEND_ESTR(start, " mov rdi, ");
|
||||
APPEND_ESTR(start, retstr);
|
||||
APPEND_ESTR(start, "\n");
|
||||
APPEND_ESTR(start, " syscall\n");
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
printf("no\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Estr complete = CREATE_ESTR("");
|
||||
APPEND_ESTR(complete, start.str);
|
||||
APPEND_ESTR(complete, data.str)
|
||||
|
||||
return complete.str;
|
||||
}
|
||||
5
src/compiler.h
Normal file
5
src/compiler.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "types.h"
|
||||
#include "interpreter.h"
|
||||
|
||||
char* compileGroundProgram(GroundProgram* program);
|
||||
[[noreturn]] void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where, int whereLine);
|
||||
52
src/include/estr.h
Normal file
52
src/include/estr.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef ESTR_H
|
||||
#define ESTR_H
|
||||
|
||||
/*
|
||||
|
||||
estr.h - Easy string manipulation
|
||||
This library has macros to allow easier manipulation of strings. No longer shall
|
||||
you have to malloc and realloc away to keep adding to your strings.
|
||||
|
||||
Usage:
|
||||
|
||||
Estr myString = CREATE_ESTR("my awesome string");
|
||||
APPEND_ESTR(myString, " is so cool");
|
||||
printf("%s\n", myString.str);
|
||||
|
||||
*/
|
||||
|
||||
#define CREATE_ESTR(instr) \
|
||||
(Estr) { \
|
||||
.str = instr,\
|
||||
.size = strlen(instr),\
|
||||
.shouldBeFreed = 0, \
|
||||
.destroyed = 0 \
|
||||
}
|
||||
|
||||
#define APPEND_ESTR(estr, instr) { \
|
||||
estr.size = estr.size + strlen(instr); \
|
||||
char* tmp_ptr = malloc(estr.size + 1); \
|
||||
if (tmp_ptr == NULL) printf("WARNING: Could not realloc estr " #estr "\n"); \
|
||||
else { \
|
||||
snprintf(tmp_ptr, estr.size + 1, "%s%s", estr.str, instr); \
|
||||
if (estr.shouldBeFreed > 0) free(estr.str); \
|
||||
estr.shouldBeFreed = 1; \
|
||||
estr.str = tmp_ptr; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define DESTROY_ESTR(estr) if (estr.shouldBeFreed > 0 && estr.destroyed < 1) free(estr.str);
|
||||
|
||||
typedef struct Estr {
|
||||
char* str;
|
||||
size_t size;
|
||||
int8_t shouldBeFreed;
|
||||
int8_t destroyed;
|
||||
} Estr;
|
||||
|
||||
#endif // ESTR_H
|
||||
@@ -1,4 +1,3 @@
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
#include "interpreter.h"
|
||||
#include "types.h"
|
||||
@@ -68,11 +67,30 @@ GroundValue groundCreateValue(GroundValueType type, ...) {
|
||||
return createListGroundValue(va_arg(args, List));
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
case FUNCTION: {
|
||||
return createFunctionGroundValue(va_arg(args, GroundFunction*));
|
||||
break;
|
||||
}
|
||||
case STRUCTVAL: {
|
||||
GroundValue gv;
|
||||
gv.type = STRUCTVAL;
|
||||
gv.data.structVal = malloc(sizeof(GroundStruct));
|
||||
*gv.data.structVal = va_arg(args, GroundStruct);
|
||||
return gv;
|
||||
}
|
||||
case NONE: {
|
||||
return createNoneGroundValue();
|
||||
break;
|
||||
}
|
||||
case ERROR:
|
||||
case CUSTOM: {
|
||||
// FIXME
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return createNoneGroundValue();
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
@@ -90,3 +108,18 @@ void groundPrintProgram(GroundProgram* program) {
|
||||
GroundProgram groundParseFile(const char* code) {
|
||||
return parseFile(code);
|
||||
}
|
||||
|
||||
GroundStruct groundCreateStruct() {
|
||||
GroundStruct gs;
|
||||
gs.size = 0;
|
||||
gs.fields = malloc(sizeof(GroundStructField));
|
||||
return gs;
|
||||
}
|
||||
|
||||
void groundAddValueToScope(GroundScope* gs, const char* name, GroundValue value) {
|
||||
addVariable(gs->variables, name, value);
|
||||
}
|
||||
|
||||
void groundAddFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field) {
|
||||
addFieldToStruct(gstruct, name, field);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,23 @@ int currentInstruction = 0;
|
||||
|
||||
bool isMainScopeGlobal = true;
|
||||
|
||||
[[noreturn]] void customError(GroundArg type, GroundArg what, GroundInstruction* where, int whereLine, int exitCode) {
|
||||
printf("Ground runtime error:\n ErrorType: ");
|
||||
printGroundArg(&type);
|
||||
printf("\n ErrorContext: ");
|
||||
printGroundArg(&what);
|
||||
printf("\n");
|
||||
if (where != NULL) {
|
||||
printf(" ErrorInstruction: ");
|
||||
printGroundInstruction(where);
|
||||
printf("\n");
|
||||
}
|
||||
if (whereLine > -1) {
|
||||
printf(" ErrorLine: %d\n", whereLine + 1);
|
||||
}
|
||||
exit(exitCode);
|
||||
}
|
||||
|
||||
[[noreturn]] void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where, int whereLine) {
|
||||
printf("Ground runtime error:\n ErrorType: ");
|
||||
switch (error) {
|
||||
@@ -335,7 +352,7 @@ GroundStruct parseStruct(GroundProgram* in, GroundScope* scope, size_t errorOffs
|
||||
|
||||
GroundValue gv;
|
||||
|
||||
switch (stringToValueType(in->instructions[i].args.args[0].value.refName)) {
|
||||
switch (stringToValueType(in->instructions[i].args.args[1].value.refName)) {
|
||||
case INT: {
|
||||
gv = createIntGroundValue(0);
|
||||
break;
|
||||
@@ -382,6 +399,7 @@ GroundStruct parseStruct(GroundProgram* in, GroundScope* scope, size_t errorOffs
|
||||
gv.type = CUSTOM;
|
||||
gv.data.customVal = malloc(sizeof(GroundObject));
|
||||
*gv.data.customVal = createObject(*gstruct);
|
||||
gv.customType = gstruct;
|
||||
break;
|
||||
}
|
||||
case NONE: {
|
||||
@@ -813,7 +831,19 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ERRORCMD: {
|
||||
if (in->args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
} else if (in->args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[2].value.value.type != INT ) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an int for arg 2", in, currentInstruction);
|
||||
}
|
||||
|
||||
customError(in->args.args[0], in->args.args[1], in, currentInstruction, in->args.args[2].value.value.data.intVal);
|
||||
break;
|
||||
}
|
||||
/*
|
||||
* VARIABLES AND LISTS
|
||||
* These instructions are for initializing variables and lists.
|
||||
@@ -1753,7 +1783,8 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
if (in->args.args[i + 1].type != VALUE) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[i + 1].value.value.type != function->args[i].type) {
|
||||
//if (in->args.args[i + 1].value.value.type != function->args[i].type) {
|
||||
if (!checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction);
|
||||
}
|
||||
appendToList(&argsList, in->args.args[i + 1].value.value);
|
||||
@@ -1781,7 +1812,8 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
if (in->args.args[i + 1].type != VALUE) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[i + 1].value.value.type != function->args[i].type) {
|
||||
//if (in->args.args[i + 1].value.value.type != function->args[i].type) {
|
||||
if (!checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction);
|
||||
}
|
||||
addVariable(newScope.variables, function->args[i].name, in->args.args[i + 1].value.value);
|
||||
@@ -1979,6 +2011,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
gv.type = CUSTOM;
|
||||
gv.data.customVal = malloc(sizeof(GroundObject));
|
||||
*gv.data.customVal = createObject(*gstruct);
|
||||
gv.customType = gstruct;
|
||||
break;
|
||||
}
|
||||
case NONE: {
|
||||
|
||||
@@ -42,6 +42,7 @@ GroundFunction* parseFunction(GroundProgram* in, size_t errorOffset);
|
||||
GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope);
|
||||
GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scope);
|
||||
|
||||
void addVariable(GroundVariable **head, const char *id, GroundValue data);
|
||||
|
||||
|
||||
|
||||
|
||||
46
src/main.c
46
src/main.c
@@ -1,5 +1,6 @@
|
||||
#include "parser.h"
|
||||
#include "interpreter.h"
|
||||
#include "compiler.h"
|
||||
#include <stdio.h>
|
||||
|
||||
char* getFileContents(const char* filename) {
|
||||
@@ -39,6 +40,7 @@ char* getFileContents(const char* filename) {
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
/*
|
||||
if (argc < 2) {
|
||||
printf("Usage: ground [file]\n");
|
||||
exit(1);
|
||||
@@ -46,5 +48,49 @@ int main(int argc, char** argv) {
|
||||
char* file = getFileContents(argv[1]);
|
||||
GroundProgram program = parseFile(file);
|
||||
free(file);
|
||||
char* compiled = compileGroundProgram(&program);
|
||||
printf("%s\n", compiled);
|
||||
interpretGroundProgram(&program, NULL);
|
||||
*/
|
||||
|
||||
if (argc < 2) {
|
||||
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
bool compile = false;
|
||||
char* fileName = NULL;
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp("--compile", argv[i]) == 0 || strcmp("-c", argv[i]) == 0) {
|
||||
compile = true;
|
||||
}
|
||||
else if (strcmp("--help", argv[i]) == 0 || strcmp("-h", argv[i]) == 0) {
|
||||
printf("GroundVM help\n");
|
||||
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
|
||||
printf("Options:\n");
|
||||
printf(" -c or --compile: Outputs Linux x86_64 assembly instead of interpreting (WIP)\n");
|
||||
printf(" -h or --help: Shows this help message\n");
|
||||
exit(0);
|
||||
} else {
|
||||
fileName = argv[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (fileName == NULL) {
|
||||
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
|
||||
printf("Error: No file name provided\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char* file = getFileContents(fileName);
|
||||
GroundProgram program = parseFile(file);
|
||||
free(file);
|
||||
|
||||
if (compile) {
|
||||
char* compiled = compileGroundProgram(&program);
|
||||
printf("%s\n", compiled);
|
||||
} else {
|
||||
interpretGroundProgram(&program, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,6 +137,7 @@ static GroundInstType getInstructionType(const char* inst) {
|
||||
if (strcmp(inst, "input") == 0 || strcmp(inst, "stdin") == 0) return INPUT;
|
||||
if (strcmp(inst, "print") == 0 || strcmp(inst, "stdout") == 0) return PRINT;
|
||||
if (strcmp(inst, "println") == 0 || strcmp(inst, "stdlnout") == 0) return PRINTLN;
|
||||
if (strcmp(inst, "error") == 0) return ERRORCMD;
|
||||
if (strcmp(inst, "set") == 0) return SET;
|
||||
if (strcmp(inst, "gettype") == 0) return GETTYPE;
|
||||
if (strcmp(inst, "exists") == 0) return EXISTS;
|
||||
|
||||
43
src/types.c
43
src/types.c
@@ -412,6 +412,9 @@ void printGroundInstruction(GroundInstruction* gi) {
|
||||
break;
|
||||
case CREATELABEL:
|
||||
break;
|
||||
case ERRORCMD:
|
||||
printf("error");
|
||||
break;
|
||||
default:
|
||||
printf("FIXME");
|
||||
break;
|
||||
@@ -582,3 +585,43 @@ GroundError createGroundError(char* what, char* type, GroundInstruction* where,
|
||||
|
||||
return ge;
|
||||
}
|
||||
|
||||
bool checkFnTypes(GroundValue* left, GroundFunctionArgs* right) {
|
||||
if (left->type != right->type) {
|
||||
return false;
|
||||
}
|
||||
if (left->type == CUSTOM) {
|
||||
if (left->customType->size != right->customType->size) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < left->customType->size; i++) {
|
||||
if (strcmp(left->customType->fields[i].id, right->customType->fields[i].id) != 0) {
|
||||
return false;
|
||||
}
|
||||
if (!checkTypes(&left->customType->fields[i].value, &right->customType->fields[i].value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool checkTypes(GroundValue* left, GroundValue* right) {
|
||||
if (left->type != right->type) {
|
||||
return false;
|
||||
}
|
||||
if (left->type == CUSTOM) {
|
||||
if (left->customType->size != right->customType->size) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < left->customType->size; i++) {
|
||||
if (strcmp(left->customType->fields[i].id, right->customType->fields[i].id) != 0) {
|
||||
return false;
|
||||
}
|
||||
if (!checkTypes(&left->customType->fields[i].value, &right->customType->fields[i].value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "include/uthash.h"
|
||||
|
||||
typedef enum GroundInstType {
|
||||
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE, DROP
|
||||
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD
|
||||
} GroundInstType;
|
||||
|
||||
typedef enum GroundValueType {
|
||||
@@ -62,6 +62,7 @@ typedef struct GroundError {
|
||||
*/
|
||||
typedef struct GroundValue {
|
||||
GroundValueType type;
|
||||
struct GroundStruct* customType;
|
||||
union {
|
||||
int64_t intVal;
|
||||
double doubleVal;
|
||||
@@ -125,6 +126,7 @@ typedef struct GroundProgram {
|
||||
*/
|
||||
typedef struct GroundFunctionArgs {
|
||||
GroundValueType type;
|
||||
struct GroundStruct* customType;
|
||||
char* name;
|
||||
} GroundFunctionArgs;
|
||||
|
||||
@@ -276,6 +278,11 @@ void freeGroundObject(GroundObject* object);
|
||||
// Creates a GroundError.
|
||||
GroundError createGroundError(char* what, char* type, GroundInstruction* where, size_t* line);
|
||||
|
||||
// Compares types of a value and function args.
|
||||
bool checkFnTypes(GroundValue* left, GroundFunctionArgs* arg);
|
||||
|
||||
// Compares types of two values.
|
||||
bool checkTypes(GroundValue* left, GroundValue* right);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
2
tests/error.grnd
Normal file
2
tests/error.grnd
Normal file
@@ -0,0 +1,2 @@
|
||||
setlist &dat 1 2 3 "Hi!"
|
||||
error "Hello" $dat 1
|
||||
Reference in New Issue
Block a user