codebased/include/scirpt/ast.h
2023-04-19 00:03:40 +02:00

208 lines
4.0 KiB
C

#ifndef SCIRPT_AST_H
#define SCIRPT_AST_H
#include "common/generic_array.h"
#include "common/string.h"
#include "common/string_array.h"
#include "scirpt/position.h"
#include <stdbool.h>
#include <stdint.h>
typedef enum {
ScirptExprTypeEof,
ScirptExprTypeError,
ScirptExprTypeId,
ScirptExprTypeInt,
ScirptExprTypeFloat,
ScirptExprTypeString,
ScirptExprTypeNull,
ScirptExprTypeBool,
ScirptExprTypeArray,
ScirptExprTypeObject,
ScirptExprTypeBlock,
ScirptExprTypeIf,
ScirptExprTypeLoop,
ScirptExprTypeWhile,
ScirptExprTypeFor,
ScirptExprTypeMember,
ScirptExprTypeCall,
ScirptExprTypeIndex,
ScirptExprTypeUnary,
ScirptExprTypeBinary,
ScirptExprTypeAssign,
ScirptExprTypeLambda,
ScirptExprTypeFunction,
ScirptExprTypeLet,
ScirptExprTypeReturn,
ScirptExprTypeBreak,
} ScirptExprType;
typedef struct ScirptExpr ScirptExpr;
GENERIC_ARRAY(ScirptExpr*, ScirptExprArray, scirpt_expr_array)
typedef struct {
HeapString key;
ScirptExpr* value;
} ScirptExprObjectEntry;
GENERIC_ARRAY(
ScirptExprObjectEntry,
ScirptExprObjectEntryArray,
scirpt_expr_object_entry_array
)
typedef struct {
ScirptExprObjectEntryArray entries;
} ScirptExprObject;
typedef struct {
ScirptExprArray statements;
} ScirptExprBlock;
typedef struct {
ScirptExpr* condition;
ScirptExpr* truthy;
ScirptExpr* falsy;
} ScirptExprIf;
typedef struct {
ScirptExpr* body;
} ScirptExprLoop;
typedef struct {
ScirptExpr* condition;
ScirptExpr* body;
} ScirptExprWhile;
typedef struct {
HeapString subject;
ScirptExpr* value;
ScirptExpr* body;
} ScirptExprFor;
typedef struct {
ScirptExpr* subject;
HeapString value;
} ScirptExprMember;
typedef struct {
ScirptExpr* subject;
ScirptExprArray args;
} ScirptExprCall;
typedef struct {
ScirptExpr* subject;
ScirptExpr* value;
} ScirptExprIndex;
typedef enum {
ScirptExprUnaryTypeNot,
ScirptExprUnaryTypeNegate,
} ScirptExprUnaryType;
typedef struct {
ScirptExprUnaryType type;
ScirptExpr* subject;
} ScirptExprUnary;
typedef enum {
ScirptExprBinaryTypeAdd,
ScirptExprBinaryTypeSubtract,
ScirptExprBinaryTypeMultiply,
ScirptExprBinaryTypeDivide,
ScirptExprBinaryTypeModulo,
ScirptExprBinaryTypeExponent,
ScirptExprBinaryTypeLt,
ScirptExprBinaryTypeGt,
ScirptExprBinaryTypeLtEqual,
ScirptExprBinaryTypeGtEqual,
ScirptExprBinaryTypeIn,
ScirptExprBinaryTypeEqual,
ScirptExprBinaryTypeInequal,
ScirptExprBinaryTypeAnd,
ScirptExprBinaryTypeOr,
} ScirptExprBinaryType;
typedef struct {
ScirptExprBinaryType type;
ScirptExpr* left;
ScirptExpr* right;
} ScirptExprBinary;
typedef enum {
ScirptExprAssignTypeEqual,
ScirptExprAssignTypeAdd,
ScirptExprAssignTypeSubtract,
ScirptExprAssignTypeMultiply,
ScirptExprAssignTypeDivide,
ScirptExprAssignTypeModulo,
ScirptExprAssignTypeExponent,
} ScirptExprAssignType;
typedef struct {
ScirptExprAssignType type;
ScirptExpr* subject;
ScirptExpr* value;
} ScirptExprAssign;
typedef struct {
HeapStringArray params;
ScirptExpr* body;
} ScirptExprLambda;
typedef struct {
HeapString subject;
HeapStringArray params;
ScirptExpr* body;
} ScirptExprFunction;
typedef struct {
HeapString subject;
ScirptExpr* value;
} ScirptExprLet;
typedef struct {
ScirptExpr* value;
} ScirptExprReturn;
typedef struct {
ScirptExpr* value;
} ScirptExprBreak;
struct ScirptExpr {
ScirptExprType type;
ScirptPosition pos;
union {
HeapString id_value;
int64_t int_value;
double float_value;
HeapString string_value;
bool bool_value;
ScirptExprArray array_value;
ScirptExprObject object;
ScirptExprBlock block;
ScirptExprIf if_expr;
ScirptExprLoop loop;
ScirptExprWhile while_expr;
ScirptExprFor for_expr;
ScirptExprMember member;
ScirptExprCall call;
ScirptExprIndex index;
ScirptExprUnary unary;
ScirptExprBinary binary;
ScirptExprAssign assign;
ScirptExprLambda lambda;
ScirptExprFunction function;
ScirptExprLet let;
ScirptExprReturn return_statement;
ScirptExprBreak break_statement;
};
};
void scirpt_expr_delete(ScirptExpr* expr);
void scirpt_expr_array_clean_and_destroy(ScirptExprArray* array);
void heapstring_array_clean_and_destroy(HeapStringArray* array);
#endif