From 2050359d2f37c7adc8c68fbe8dddb240eb13b3c6 Mon Sep 17 00:00:00 2001 From: cpunch Date: Mon, 15 Feb 2021 16:20:04 -0600 Subject: [PATCH] Added C99 support, refactored headers --- Makefile | 2 +- docs/intro.md | 2 +- src/cchunk.c | 1 + src/cchunk.h | 6 ++---- src/cobj.h | 44 ++++++++++++++++++++------------------------ src/cosmo.h | 5 +++++ src/cstate.h | 8 ++++---- src/cvalue.h | 12 ++++++------ 8 files changed, 40 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index 1190535..6619cfa 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # make clean && make && ./bin/cosmo CC=clang -CFLAGS=-fPIE -Wall -O3 -Isrc -std=c11 +CFLAGS=-fPIE -Wall -O3 -Isrc -std=c99 -Werror LDFLAGS=-lm #-fsanitize=address OUT=bin/cosmo diff --git a/docs/intro.md b/docs/intro.md index d60d5ea..8ea40b2 100644 --- a/docs/intro.md +++ b/docs/intro.md @@ -1,6 +1,6 @@ # Introduction -Cosmo is a lightweight embeddable scripting language written in C11. Cosmo has comparable syntax to Lua 5.1, so if you are familiar with that syntax, learning Cosmo should be trivial. Cosmo has eccentric support for object-oriented programming, procedural programming, and functional programming. To see some examples that highlight the syntax, please see the `examples/` directory. +Cosmo is a lightweight embeddable scripting language written in C99. Cosmo has comparable syntax to Lua 5.1, so if you are familiar with that syntax, learning Cosmo should be trivial. Cosmo has eccentric support for object-oriented programming, procedural programming, and functional programming. To see some examples that highlight the syntax, please see the `examples/` directory. As Cosmo is an embeddable scripting language, it is designed to be extended by the host program (from here on referenced as 'host'.) Cosmo provides extensive C API for the host to set up the Cosmo VM, modify state, add custom Proto objects, define custom globals and more. diff --git a/src/cchunk.c b/src/cchunk.c index 858e4e7..7d58b0e 100644 --- a/src/cchunk.c +++ b/src/cchunk.c @@ -2,6 +2,7 @@ #include "cchunk.h" #include "cvalue.h" #include "cvm.h" +#include "cobj.h" CChunk *newChunk(CState* state, size_t startCapacity) { CChunk *chunk = cosmoM_xmalloc(state, sizeof(CChunk)); diff --git a/src/cchunk.h b/src/cchunk.h index e4b42f8..8f97636 100644 --- a/src/cchunk.h +++ b/src/cchunk.h @@ -6,16 +6,14 @@ #include "coperators.h" #include "cvalue.h" -typedef struct CValueArray CValueArray; - -typedef struct CChunk { +struct CChunk { size_t capacity; // the amount of space we've allocated for size_t count; // the space we're currently using INSTRUCTION *buf; // whole chunk CValueArray constants; // holds constants size_t lineCapacity; int *lineInfo; -} CChunk; +}; CChunk *newChunk(CState* state, size_t startCapacity); void initChunk(CState* state, CChunk *chunk, size_t startCapacity); diff --git a/src/cobj.h b/src/cobj.h index b5fde5f..47bffec 100644 --- a/src/cobj.h +++ b/src/cobj.h @@ -22,42 +22,38 @@ typedef enum CObjType { #include "cvalue.h" #include "ctable.h" -typedef struct CState CState; -typedef struct CCallFrame CCallFrame; -typedef uint32_t cosmo_Flag; - #define CommonHeader CObj _obj #define readFlag(x, flag) (x & (1u << flag)) #define setFlagOn(x, flag) (x |= (1u << flag)) typedef int (*CosmoCFunction)(CState *state, int argCount, CValue *args); -typedef struct CObj { +struct CObj { struct CObj *next; struct CObj *nextRoot; // for the root linked list struct CObjObject *proto; // protoobject, describes the behavior of the object CObjType type; bool isMarked; // for the GC -} CObj; +}; -typedef struct CObjString { +struct CObjString { CommonHeader; // "is a" CObj char *str; // NULL termincated string uint32_t hash; // for hashtable lookup int length; bool isIString; -} CObjString; +}; -typedef struct CObjError { +struct CObjError { CommonHeader; // "is a" CObj CValue err; // error string CCallFrame *frames; int frameCount; int line; // reserved for parser errors bool parserError; // if true, cosmoV_printError will format the error to the lexer -} CObjError; +}; -typedef struct CObjObject { +struct CObjObject { CommonHeader; // "is a" CObj CTable tbl; cosmo_Flag istringFlags; // enables us to have a much faster lookup for reserved IStrings (like __init, __index, etc.) @@ -67,14 +63,14 @@ typedef struct CObjObject { }; int userT; // user-defined type (for describing the userdata pointer/integer) bool isLocked; -} CObjObject; +}; -typedef struct CObjTable { // table, a wrapper for CTable +struct CObjTable { // table, a wrapper for CTable CommonHeader; // "is a" CObj CTable tbl; -} CObjTable; +}; -typedef struct CObjFunction { +struct CObjFunction { CommonHeader; // "is a" CObj CChunk chunk; CObjString *name; @@ -82,32 +78,32 @@ typedef struct CObjFunction { int args; int upvals; bool variadic; -} CObjFunction; +}; -typedef struct CObjCFunction { +struct CObjCFunction { CommonHeader; // "is a" CObj CosmoCFunction cfunc; -} CObjCFunction; +}; -typedef struct CObjClosure { +struct CObjClosure { CommonHeader; // "is a" CObj CObjFunction *function; CObjUpval **upvalues; int upvalueCount; -} CObjClosure; +}; -typedef struct CObjMethod { +struct CObjMethod { CommonHeader; // "is a " CObj CValue func; CObj *obj; // obj this method is bound too -} CObjMethod; +}; -typedef struct CObjUpval { +struct CObjUpval { CommonHeader; // "is a" CObj CValue closed; CValue *val; struct CObjUpval *next; -} CObjUpval; +}; #undef CommonHeader diff --git a/src/cosmo.h b/src/cosmo.h index 37a67ff..3e5f457 100644 --- a/src/cosmo.h +++ b/src/cosmo.h @@ -20,12 +20,17 @@ // forward declare *most* stuff so our headers are cleaner typedef struct CState CState; typedef struct CChunk CChunk; +typedef struct CCallFrame CCallFrame; + #ifdef NAN_BOXXED typedef union CValue CValue; #else typedef struct CValue CValue; #endif +typedef struct CValueArray CValueArray; +typedef uint32_t cosmo_Flag; + // objs typedef struct CObj CObj; typedef struct CObjString CObjString; diff --git a/src/cstate.h b/src/cstate.h index ded8cae..353a066 100644 --- a/src/cstate.h +++ b/src/cstate.h @@ -6,11 +6,11 @@ #include "cvalue.h" #include "ctable.h" -typedef struct CCallFrame { +struct CCallFrame { CObjClosure *closure; INSTRUCTION *pc; CValue* base; -} CCallFrame; +}; typedef enum IStringEnum { ISTRING_INIT, // __init @@ -33,7 +33,7 @@ typedef struct ArrayCObj { int capacity; } ArrayCObj; -typedef struct CState { +struct CState { bool panic; int freezeGC; // when > 0, GC events will be ignored (for internal use) int frameCount; @@ -54,7 +54,7 @@ typedef struct CState { CObjString *iStrings[ISTRING_MAX]; // strings used internally by the VM, eg. __init, __index & friends CCallFrame callFrame[FRAME_MAX]; // call frames CValue stack[STACK_MAX]; // stack -} CState; +}; COSMO_API CState *cosmoV_newState(); // expects 2*pairs values on the stack, each pair should consist of 1 key and 1 value diff --git a/src/cvalue.h b/src/cvalue.h index 94e0790..0ee8895 100644 --- a/src/cvalue.h +++ b/src/cvalue.h @@ -27,10 +27,10 @@ typedef double cosmo_Number; TL;DR: we can store payloads in the NaN value in the IEEE 754 standard. */ -typedef union CValue { +union CValue { uint64_t data; cosmo_Number num; -} CValue; +}; #define MASK_TYPE ((uint64_t)0x0007000000000000) #define MASK_PAYLOAD ((uint64_t)0x0000ffffffffffff) @@ -68,14 +68,14 @@ typedef union CValue { /* Tagged union, this is the best platform independent solution */ -typedef struct CValue { +struct CValue { CosmoType type; union { cosmo_Number num; bool b; // boolean CObj *obj; } val; -} CValue; +}; #define GET_TYPE(x) ((x).type) @@ -103,11 +103,11 @@ typedef struct CValue { typedef CValue* StkPtr; -typedef struct CValueArray { +struct CValueArray { size_t capacity; size_t count; CValue *values; -} CValueArray; +}; void initValArray(CState *state, CValueArray *val, size_t startCapacity); void cleanValArray(CState *state, CValueArray *array); // cleans array