aboutsummaryrefslogtreecommitdiff
path: root/src/compiler/code_generator/code_generator.h
blob: 902d76b034bf64654feeee5bf4562c8ccdf32b36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once

#include <compiler/parser/parser.h>

#include "compiler/tree_parser/tree_parser.h"
#include "utils/types.h"

typedef enum Command {
  COMMAND_NONE = 0,
  COMMAND_CALL_FUNCTION,
  COMMAND_PUSH_STRING,
  COMMAND_PUSH_IDENTIFIER,
  COMMAND_POP_IDENTIFIER,
} Command;

extern const char *COMMAND_STRINGS[];

typedef struct Instruction {
  Command command;
  void *operand;
} Instruction;

typedef struct Instructions {
  Instruction *restrict instructions;
  size_t size;
} Instructions;

typedef SizedString CommandCallFunctionOperand;
typedef SizedString CommandPushStringOperand;
typedef SizedString CommandPushIdentifierOperand;
typedef SizedString CommandPopIdentifierOperand;

extern void printInstruction(Instruction instruction);
extern void printInstructions(Instructions instructions);

extern void deleteInstruction(Instruction instruction);
extern void deleteInstructions(Instructions instructions);

extern Instructions codeGenerator(SourceCode *code);
extern Instructions codeGeneratorWithPrint(SourceCode *code);
extern Instructions _codeGenerator(ParsedTree *root, SourceCode *code);

extern bool nodeToInstruction(ParsedTree *tree, Instruction **instructions,
                              size_t *instructions_size,
                              size_t *instructions_inserted, SourceCode *code);

extern void insertInstruction(const Instruction instruction,
                              Instruction **restrict instructions,
                              size_t *restrict instructions_size,
                              size_t *restrict instructions_inserted);