2021-02-07 15:40:00 +08:00
|
|
|
/*
|
2021-06-09 18:42:26 +08:00
|
|
|
* Copyright (c) 2020-2021 Thakee Nathees
|
|
|
|
* Distributed Under The MIT License
|
2021-02-07 15:40:00 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COMPILER_H
|
|
|
|
#define COMPILER_H
|
|
|
|
|
2021-06-09 18:42:26 +08:00
|
|
|
#include "pk_common.h"
|
|
|
|
#include "pk_var.h"
|
2021-02-07 15:40:00 +08:00
|
|
|
|
2021-06-06 22:24:13 +08:00
|
|
|
typedef enum {
|
|
|
|
#define OPCODE(name, _, __) OP_##name,
|
2021-06-09 18:42:26 +08:00
|
|
|
#include "pk_opcodes.h"
|
2021-06-06 22:24:13 +08:00
|
|
|
#undef OPCODE
|
|
|
|
} Opcode;
|
|
|
|
|
2021-06-10 06:40:24 +08:00
|
|
|
// Pocketlang compiler is a one pass/single pass compiler, which means it
|
|
|
|
// doesn't go through the basic compilation pipeline such as lexing, parsing
|
|
|
|
// (AST), analyzing, intermediate code generation, and target codegeneration
|
|
|
|
// one by one. Instead it'll generate the target code as it reads the source
|
2021-06-10 07:35:14 +08:00
|
|
|
// (directly from lexing to codegen). Despite it's faster than multipass
|
|
|
|
// compilers, we're restricted syntax-wise and from compile-time optimizations.
|
|
|
|
// Yet we support "forward names" to call functions before they defined
|
|
|
|
// (unlike C/Python).
|
2021-02-07 15:40:00 +08:00
|
|
|
typedef struct Compiler Compiler;
|
|
|
|
|
2021-06-01 19:50:41 +08:00
|
|
|
// This will take source code as a cstring, compiles it to pocketlang bytecodes
|
2021-06-07 13:54:06 +08:00
|
|
|
// and append them to the script's implicit main function ("$(SourceBody)").
|
2021-06-09 18:42:26 +08:00
|
|
|
// On a successfull compilation it'll return PK_RESULT_SUCCESS, otherwise it'll
|
|
|
|
// return PK_RESULT_COMPILE_ERROR but if repl_mode set in the [options], and
|
|
|
|
// we've reached and unexpected EOF it'll return PK_RESULT_UNEXPECTED_EOF.
|
|
|
|
PkResult compile(PKVM* vm, Script* script, const char* source,
|
|
|
|
const PkCompileOptions* options);
|
2021-02-07 15:40:00 +08:00
|
|
|
|
2021-06-15 15:37:49 +08:00
|
|
|
// Mark the heap allocated objects of the compiler at the garbage collection
|
2021-06-01 19:50:41 +08:00
|
|
|
// called at the marking phase of vmCollectGarbage().
|
2021-05-19 02:59:09 +08:00
|
|
|
void compilerMarkObjects(PKVM* vm, Compiler* compiler);
|
2021-04-26 17:34:30 +08:00
|
|
|
|
2021-02-07 15:40:00 +08:00
|
|
|
#endif // COMPILER_H
|