pocketlang/src/pk_compiler.h

42 lines
1.6 KiB
C
Raw Normal View History

2021-02-07 15:40:00 +08:00
/*
* Copyright (c) 2020-2022 Thakee Nathees
* Copyright (c) 2021-2022 Pocketlang Contributors
2021-06-09 18:42:26 +08:00
* Distributed Under The MIT License
2021-02-07 15:40:00 +08:00
*/
#ifndef PK_COMPILER_H
#define PK_COMPILER_H
2021-02-07 15:40:00 +08:00
#include "pk_internal.h"
#include "pk_value.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;
// 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
// (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
// 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
#endif // PK_COMPILER_H