mirror of
https://github.com/CPunch/Cosmo.git
synced 2024-11-24 16:11:04 +00:00
Added C99 support, refactored headers
This commit is contained in:
parent
71c8dc7e34
commit
35466f691f
2
Makefile
2
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
|
||||
|
||||
|
@ -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.
|
||||
|
||||
|
@ -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));
|
||||
|
@ -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);
|
||||
|
44
src/cobj.h
44
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
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
12
src/cvalue.h
12
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
|
||||
|
Loading…
Reference in New Issue
Block a user