Added C99 support, refactored headers

This commit is contained in:
cpunch 2021-02-15 16:20:04 -06:00
parent 47051575cb
commit 2050359d2f
8 changed files with 40 additions and 40 deletions

View File

@ -1,7 +1,7 @@
# make clean && make && ./bin/cosmo # make clean && make && ./bin/cosmo
CC=clang CC=clang
CFLAGS=-fPIE -Wall -O3 -Isrc -std=c11 CFLAGS=-fPIE -Wall -O3 -Isrc -std=c99 -Werror
LDFLAGS=-lm #-fsanitize=address LDFLAGS=-lm #-fsanitize=address
OUT=bin/cosmo OUT=bin/cosmo

View File

@ -1,6 +1,6 @@
# Introduction # 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. 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.

View File

@ -2,6 +2,7 @@
#include "cchunk.h" #include "cchunk.h"
#include "cvalue.h" #include "cvalue.h"
#include "cvm.h" #include "cvm.h"
#include "cobj.h"
CChunk *newChunk(CState* state, size_t startCapacity) { CChunk *newChunk(CState* state, size_t startCapacity) {
CChunk *chunk = cosmoM_xmalloc(state, sizeof(CChunk)); CChunk *chunk = cosmoM_xmalloc(state, sizeof(CChunk));

View File

@ -6,16 +6,14 @@
#include "coperators.h" #include "coperators.h"
#include "cvalue.h" #include "cvalue.h"
typedef struct CValueArray CValueArray; struct CChunk {
typedef struct CChunk {
size_t capacity; // the amount of space we've allocated for size_t capacity; // the amount of space we've allocated for
size_t count; // the space we're currently using size_t count; // the space we're currently using
INSTRUCTION *buf; // whole chunk INSTRUCTION *buf; // whole chunk
CValueArray constants; // holds constants CValueArray constants; // holds constants
size_t lineCapacity; size_t lineCapacity;
int *lineInfo; int *lineInfo;
} CChunk; };
CChunk *newChunk(CState* state, size_t startCapacity); CChunk *newChunk(CState* state, size_t startCapacity);
void initChunk(CState* state, CChunk *chunk, size_t startCapacity); void initChunk(CState* state, CChunk *chunk, size_t startCapacity);

View File

@ -22,42 +22,38 @@ typedef enum CObjType {
#include "cvalue.h" #include "cvalue.h"
#include "ctable.h" #include "ctable.h"
typedef struct CState CState;
typedef struct CCallFrame CCallFrame;
typedef uint32_t cosmo_Flag;
#define CommonHeader CObj _obj #define CommonHeader CObj _obj
#define readFlag(x, flag) (x & (1u << flag)) #define readFlag(x, flag) (x & (1u << flag))
#define setFlagOn(x, flag) (x |= (1u << flag)) #define setFlagOn(x, flag) (x |= (1u << flag))
typedef int (*CosmoCFunction)(CState *state, int argCount, CValue *args); typedef int (*CosmoCFunction)(CState *state, int argCount, CValue *args);
typedef struct CObj { struct CObj {
struct CObj *next; struct CObj *next;
struct CObj *nextRoot; // for the root linked list struct CObj *nextRoot; // for the root linked list
struct CObjObject *proto; // protoobject, describes the behavior of the object struct CObjObject *proto; // protoobject, describes the behavior of the object
CObjType type; CObjType type;
bool isMarked; // for the GC bool isMarked; // for the GC
} CObj; };
typedef struct CObjString { struct CObjString {
CommonHeader; // "is a" CObj CommonHeader; // "is a" CObj
char *str; // NULL termincated string char *str; // NULL termincated string
uint32_t hash; // for hashtable lookup uint32_t hash; // for hashtable lookup
int length; int length;
bool isIString; bool isIString;
} CObjString; };
typedef struct CObjError { struct CObjError {
CommonHeader; // "is a" CObj CommonHeader; // "is a" CObj
CValue err; // error string CValue err; // error string
CCallFrame *frames; CCallFrame *frames;
int frameCount; int frameCount;
int line; // reserved for parser errors int line; // reserved for parser errors
bool parserError; // if true, cosmoV_printError will format the error to the lexer bool parserError; // if true, cosmoV_printError will format the error to the lexer
} CObjError; };
typedef struct CObjObject { struct CObjObject {
CommonHeader; // "is a" CObj CommonHeader; // "is a" CObj
CTable tbl; CTable tbl;
cosmo_Flag istringFlags; // enables us to have a much faster lookup for reserved IStrings (like __init, __index, etc.) 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) int userT; // user-defined type (for describing the userdata pointer/integer)
bool isLocked; bool isLocked;
} CObjObject; };
typedef struct CObjTable { // table, a wrapper for CTable struct CObjTable { // table, a wrapper for CTable
CommonHeader; // "is a" CObj CommonHeader; // "is a" CObj
CTable tbl; CTable tbl;
} CObjTable; };
typedef struct CObjFunction { struct CObjFunction {
CommonHeader; // "is a" CObj CommonHeader; // "is a" CObj
CChunk chunk; CChunk chunk;
CObjString *name; CObjString *name;
@ -82,32 +78,32 @@ typedef struct CObjFunction {
int args; int args;
int upvals; int upvals;
bool variadic; bool variadic;
} CObjFunction; };
typedef struct CObjCFunction { struct CObjCFunction {
CommonHeader; // "is a" CObj CommonHeader; // "is a" CObj
CosmoCFunction cfunc; CosmoCFunction cfunc;
} CObjCFunction; };
typedef struct CObjClosure { struct CObjClosure {
CommonHeader; // "is a" CObj CommonHeader; // "is a" CObj
CObjFunction *function; CObjFunction *function;
CObjUpval **upvalues; CObjUpval **upvalues;
int upvalueCount; int upvalueCount;
} CObjClosure; };
typedef struct CObjMethod { struct CObjMethod {
CommonHeader; // "is a " CObj CommonHeader; // "is a " CObj
CValue func; CValue func;
CObj *obj; // obj this method is bound too CObj *obj; // obj this method is bound too
} CObjMethod; };
typedef struct CObjUpval { struct CObjUpval {
CommonHeader; // "is a" CObj CommonHeader; // "is a" CObj
CValue closed; CValue closed;
CValue *val; CValue *val;
struct CObjUpval *next; struct CObjUpval *next;
} CObjUpval; };
#undef CommonHeader #undef CommonHeader

View File

@ -20,12 +20,17 @@
// forward declare *most* stuff so our headers are cleaner // forward declare *most* stuff so our headers are cleaner
typedef struct CState CState; typedef struct CState CState;
typedef struct CChunk CChunk; typedef struct CChunk CChunk;
typedef struct CCallFrame CCallFrame;
#ifdef NAN_BOXXED #ifdef NAN_BOXXED
typedef union CValue CValue; typedef union CValue CValue;
#else #else
typedef struct CValue CValue; typedef struct CValue CValue;
#endif #endif
typedef struct CValueArray CValueArray;
typedef uint32_t cosmo_Flag;
// objs // objs
typedef struct CObj CObj; typedef struct CObj CObj;
typedef struct CObjString CObjString; typedef struct CObjString CObjString;

View File

@ -6,11 +6,11 @@
#include "cvalue.h" #include "cvalue.h"
#include "ctable.h" #include "ctable.h"
typedef struct CCallFrame { struct CCallFrame {
CObjClosure *closure; CObjClosure *closure;
INSTRUCTION *pc; INSTRUCTION *pc;
CValue* base; CValue* base;
} CCallFrame; };
typedef enum IStringEnum { typedef enum IStringEnum {
ISTRING_INIT, // __init ISTRING_INIT, // __init
@ -33,7 +33,7 @@ typedef struct ArrayCObj {
int capacity; int capacity;
} ArrayCObj; } ArrayCObj;
typedef struct CState { struct CState {
bool panic; bool panic;
int freezeGC; // when > 0, GC events will be ignored (for internal use) int freezeGC; // when > 0, GC events will be ignored (for internal use)
int frameCount; int frameCount;
@ -54,7 +54,7 @@ typedef struct CState {
CObjString *iStrings[ISTRING_MAX]; // strings used internally by the VM, eg. __init, __index & friends CObjString *iStrings[ISTRING_MAX]; // strings used internally by the VM, eg. __init, __index & friends
CCallFrame callFrame[FRAME_MAX]; // call frames CCallFrame callFrame[FRAME_MAX]; // call frames
CValue stack[STACK_MAX]; // stack CValue stack[STACK_MAX]; // stack
} CState; };
COSMO_API CState *cosmoV_newState(); COSMO_API CState *cosmoV_newState();
// expects 2*pairs values on the stack, each pair should consist of 1 key and 1 value // expects 2*pairs values on the stack, each pair should consist of 1 key and 1 value

View File

@ -27,10 +27,10 @@ typedef double cosmo_Number;
TL;DR: we can store payloads in the NaN value in the IEEE 754 standard. TL;DR: we can store payloads in the NaN value in the IEEE 754 standard.
*/ */
typedef union CValue { union CValue {
uint64_t data; uint64_t data;
cosmo_Number num; cosmo_Number num;
} CValue; };
#define MASK_TYPE ((uint64_t)0x0007000000000000) #define MASK_TYPE ((uint64_t)0x0007000000000000)
#define MASK_PAYLOAD ((uint64_t)0x0000ffffffffffff) #define MASK_PAYLOAD ((uint64_t)0x0000ffffffffffff)
@ -68,14 +68,14 @@ typedef union CValue {
/* /*
Tagged union, this is the best platform independent solution Tagged union, this is the best platform independent solution
*/ */
typedef struct CValue { struct CValue {
CosmoType type; CosmoType type;
union { union {
cosmo_Number num; cosmo_Number num;
bool b; // boolean bool b; // boolean
CObj *obj; CObj *obj;
} val; } val;
} CValue; };
#define GET_TYPE(x) ((x).type) #define GET_TYPE(x) ((x).type)
@ -103,11 +103,11 @@ typedef struct CValue {
typedef CValue* StkPtr; typedef CValue* StkPtr;
typedef struct CValueArray { struct CValueArray {
size_t capacity; size_t capacity;
size_t count; size_t count;
CValue *values; CValue *values;
} CValueArray; };
void initValArray(CState *state, CValueArray *val, size_t startCapacity); void initValArray(CState *state, CValueArray *val, size_t startCapacity);
void cleanValArray(CState *state, CValueArray *array); // cleans array void cleanValArray(CState *state, CValueArray *array); // cleans array