shithub: lpa

ref: 2b23d05d57743af57385cd42c0fd2d223b11d8c8
dir: /dat.h/

View raw version
enum DataTag
{
	DataAux,
	DataSession,
	DataSessionList,
	DataModule,
	DataModuleList,
	DataSymtab,
	DataSymbol,
	DataEnumeration,
	DataTokenList,
	DataArray,
	DataAst,
	DataByteCode,
	DataValueStack,
	DataCallStack,
	DataFunction,
	DataLocalList,
	DataErrorCtx,
	DataErrorTrap,
	DataConstraint,
	DataConstraintVar,
	DataConstraintGraph,

	DataMax,
};

typedef struct DataSpec DataSpec;
struct DataSpec
{
	usize size;
};
extern DataSpec dataspecs[DataMax]; /* memory.c */

typedef struct Symbol Symbol;
typedef struct Symtab Symtab;

struct Symbol
{
	char *name;
	void *value;
	Qid qsymbol;

	Symtab *table;
	uvlong id;
};

struct Symtab
{
	RWLock lock;

	uvlong count;
	Symbol **symbols;
};

typedef struct Module Module;
struct Module
{
	uvlong id;
	char *name;

	Symtab *symtab;
	Qid qsession;
	Qid qmodule;
};

typedef struct ModuleList ModuleList;
struct ModuleList
{
	RWLock lock;
	uvlong count;
	Module **modules;
};

typedef struct Session Session;	
struct Session
{
	uvlong id;
	char *name;

	int active; /* is the session alive? */

	ModuleList *modules;

	/* file server stuff */
	Qid qsession;
	Qid qctl;
	Qid qcons;
	Qid qlog;
	Qid qmodules;
	Qid qthreads;

	QLock loglock;
	Rendez logwait;

	uvlong logsize;
	char *log;

	Channel *input;
};

typedef struct Enumeration Enumeration;
struct Enumeration
{
	uvlong count;
	void **items;
};

enum TokenTag
{
	TokNumber,
	TokName,
	TokLparen,
	TokRparen,
	TokLbrack,
	TokRbrack,
	TokLbrace,
	TokRbrace,
	TokNewline,
	TokDiamond,
	TokPrimitive,
	TokDel,
	TokLarrow,
	TokSemi,
	TokString,

	TokEnd,
};

typedef struct Token Token;
struct Token
{
	int tag;
	int nameclass;
	union {
		vlong num; /* TokNumber */
		char *name; /* TokName: UTF-8 encoded name */
		Rune *string; /* TokString: string contents */
		int prim; /* TokPrimitive */
	};
};

typedef struct TokenList TokenList;
struct TokenList
{
	uvlong count;
	Token *tokens;

	uvlong offset;
};

enum ArrayType
{
	TypeNumber,
	TypeChar,
	TypeArray,
	TypeVar,
};

typedef struct Array Array;
#pragma incomplete Array

enum AstTag
{
	AstProg,
	AstFunc,
	AstName,
	AstLocals,
	AstAssign,
	AstNiladic,
	AstMonadic,
	AstDyadic,
	AstConst,
	AstPrim,
	AstStrand,
	AstLater, /* parse at runtime */
};

typedef struct ByteCode ByteCode;

typedef struct Ast Ast;
struct Ast
{
	int tag;
	char *name;
	int nameclass;
	int prim;
	int optional; /* optional left arg */

	Ast *funcname;
	Ast *funcresult;
	Ast *funcleftarg;
	Ast *funcrightarg;
	Ast *funclocals;

	Ast *func;
	Ast *left;
	Ast *right;

	Array *val;

	TokenList *tokens; /* for AstLater */

	uvlong childcount;
	Ast **children;
};

enum Nameclass
{
	NameclassUndef, /* Unknown name */
	NameclassLocal, /* Local variable, but no value yet */
	NameclassArray, /* Array value */
	NameclassFunc, /* Function value */
};

struct ByteCode
{
	uvlong count;
	u8int *instrs;
};

enum Instr
{
	IPushConst,
	IPushPrim,
	ILookup,
	IStrand,
	INiladic,
	IMonadic,
	IDyadic,
	IParse,
	IReturn,
	IAssign,
	ILocal,
	IPop,
	IDisplay,
	IPushVar,
};

typedef struct ValueStack ValueStack;
struct ValueStack
{
	uvlong count;
	void **values;
};

typedef struct Local Local;
struct Local
{
	uvlong id;
	void *value;
};

typedef struct LocalList LocalList;
struct LocalList
{
	uvlong count;
	Local *list;
};

typedef struct CallFrame CallFrame;
struct CallFrame
{
	/* Values stored when the frame is pushed */
	ByteCode *code;
	uvlong offset;

	/* Old values of symbols before they were localised */
	LocalList *locals;
};

typedef struct CallStack CallStack;
struct CallStack
{
	uvlong count;
	CallFrame *frames;
};

enum Valence
{
	Niladic = 1,
	Monadic = 2,
	Dyadic = 4,
	Variadic = 6,
};

typedef struct Function Function;
struct Function
{
	Ast *ast;
	int valence;
	int hasresult;

	uvlong symbol;
	ByteCode *code;
	int prim;
};

enum ErrorNum
{
	EAny, /* 0 = catch any error */
	ESyntax,
	EValue,
	EInternal,
	EDomain,

	ErrorMax,
};

typedef struct ErrorTrap ErrorTrap;
struct ErrorTrap
{
	jmp_buf env;
	int nums[ErrorMax];
};

typedef struct ErrorCtx ErrorCtx;
struct ErrorCtx
{
	char msg[4096];
	int num;

	uvlong count;
	ErrorTrap **traps;
};

enum ConstraintType
{
	CEqual,
};

typedef struct Constraint Constraint;
typedef struct ConstraintVar ConstraintVar;

struct Constraint
{
	int tag;
	Ast *ast;

	Array *left;
	Array *right;

	ConstraintVar *vars[2]; /* max 2 vars for now */
};

struct ConstraintVar
{
	char *name;
	int id;

	Ast *ast;

	uvlong count;
	Constraint **constraints;
};

enum PrimitiveId
{
	PRight,
	PLeft,
	PPlus,
	PMinus,
	PRho,
	PMatch,

	PAssert,
	PAll,
	PSolve,
	PVar,
};

typedef struct ConstraintGraph ConstraintGraph;
struct ConstraintGraph
{
	uvlong vcount;
	uvlong ccount;

	ConstraintVar *vs[128];
	Constraint *cs[128];
};