pocketlang/src/pk_compiler.h

40 lines
1.5 KiB
C
Raw Normal View History

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
typedef enum {
#define OPCODE(name, _, __) OP_##name,
2021-06-09 18:42:26 +08:00
#include "pk_opcodes.h"
#undef OPCODE
} Opcode;
2021-06-01 19:50:41 +08:00
// Pocketlang compiler is a single pass compiler, which means it doesn't go
// throught the basic compilation pipline such as lexing, parsing (AST),
// analyzing, intermediate codegeneration, and target codegeneration one by one
// instead it'll generate the target code as it reads the source (directly from
// lexing to codegen). Despite it's faster than multipass compilers, we're
// restricted syntax-wise and from compiletime 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-01 19:50:41 +08:00
// Mark the heap allocated ojbects of the compiler at the garbage collection
// 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