mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
add pikaCompier.c and .h
This commit is contained in:
parent
31bef9dbce
commit
ea4776ae22
@ -1200,11 +1200,13 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
|
||||
block_deepth_last = stack_getTop(block_stack);
|
||||
/* exit each block */
|
||||
for (int i = 0; i < block_deepth_last - block_deepth_now; i++) {
|
||||
QueueObj* exit_block_queue = obj_getObjWithKeepDeepth(ast, "exitBlock", 0);
|
||||
QueueObj* exit_block_queue =
|
||||
obj_getObjWithKeepDeepth(ast, "exitBlock", 0);
|
||||
/* create an exit_block queue */
|
||||
if (NULL == exit_block_queue) {
|
||||
obj_newObj(ast, "exitBlock", "", New_TinyObj);
|
||||
exit_block_queue = obj_getObjWithKeepDeepth(ast, "exitBlock", 0);
|
||||
exit_block_queue =
|
||||
obj_getObjWithKeepDeepth(ast, "exitBlock", 0);
|
||||
queueObj_init(exit_block_queue);
|
||||
}
|
||||
char buff[10] = {0};
|
||||
@ -1497,9 +1499,9 @@ static uint8_t Parser_checkIsMultiComment(char* line) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char* Parser_parsePyLines(Args* outBuffs,
|
||||
ByteCodeFrame* bytecode_frame,
|
||||
char* py_lines) {
|
||||
char* Parser_parsePyLines(Args* outBuffs,
|
||||
ByteCodeFrame* bytecode_frame,
|
||||
char* py_lines) {
|
||||
Stack block_stack;
|
||||
stack_init(&block_stack);
|
||||
Arg* asm_buff = arg_setStr(NULL, "", "");
|
||||
@ -1985,81 +1987,3 @@ void Parser_compilePyToBytecodeArray(char* lines) {
|
||||
/* deinit */
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
}
|
||||
|
||||
/* const Pool output redirect */
|
||||
static void __handler_constPool_output_file(ConstPool* self, char* content) {
|
||||
/* to ram */
|
||||
uint16_t size = strGetSize(content) + 1;
|
||||
self->arg_buff = arg_append(self->arg_buff, content, size);
|
||||
/* to flash */
|
||||
__platform_fwrite(content, 1, size, self->output_f);
|
||||
}
|
||||
|
||||
/* instruct array output redirect */
|
||||
static void __handler_instructArray_output_none(InstructArray* self,
|
||||
InstructUnit* ins_unit) {
|
||||
/* none */
|
||||
}
|
||||
|
||||
static void __handler_instructArray_output_file(InstructArray* self,
|
||||
InstructUnit* ins_unit) {
|
||||
/* to flash */
|
||||
__platform_fwrite(ins_unit, 1, instructUnit_getSize(), self->output_f);
|
||||
}
|
||||
|
||||
/*
|
||||
need implament :
|
||||
__platform_fopen()
|
||||
__platform_fwrite()
|
||||
__platform_fclose()
|
||||
*/
|
||||
int pikaCompile(char* output_file_name, char* py_lines) {
|
||||
ByteCodeFrame bytecode_frame = {0};
|
||||
|
||||
FILE* bytecode_f = __platform_fopen(output_file_name, "w+");
|
||||
/* main process */
|
||||
|
||||
/* step 1, get size of const pool and instruct array */
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_none;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
uint16_t const_pool_size = bytecode_frame.const_pool.size;
|
||||
uint16_t instruct_array_size = bytecode_frame.instruct_array.size;
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* step 2, write instruct array to file */
|
||||
__platform_fwrite(&instruct_array_size, 1, 2, bytecode_f);
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
/* instruct array to file */
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_file;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* step 3, write const pool to file */
|
||||
__platform_fwrite(&const_pool_size, 1, 2, bytecode_f);
|
||||
char void_ = 0;
|
||||
/* add \0 at the start */
|
||||
__platform_fwrite(&void_, 1, 1, bytecode_f);
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
/* const pool to file */
|
||||
bytecode_frame.const_pool.output_redirect_fun =
|
||||
__handler_constPool_output_file;
|
||||
/* instruct array to none */
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_none;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* deinit */
|
||||
__platform_fclose(bytecode_f);
|
||||
/* succeed */
|
||||
return 0;
|
||||
};
|
||||
|
@ -83,6 +83,9 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* bf, char* pikaAsm);
|
||||
int bytecodeFrame_fromMultiLine(ByteCodeFrame* bytecode_frame,
|
||||
char* python_lines);
|
||||
void Parser_compilePyToBytecodeArray(char* lines);
|
||||
char* Parser_parsePyLines(Args* outBuffs,
|
||||
ByteCodeFrame* bytecode_frame,
|
||||
char* py_lines);
|
||||
#define ParserState_forEach(parseState) \
|
||||
ParserState_beforeIter(&parseState); \
|
||||
for (int i = 0; i < parseState.length; i++)
|
||||
|
87
port/linux/package/pikascript/pikascript-core/pikaCompiler.c
Normal file
87
port/linux/package/pikascript/pikascript-core/pikaCompiler.c
Normal file
@ -0,0 +1,87 @@
|
||||
#include "PikaParser.h"
|
||||
#include "BaseObj.h"
|
||||
#include "PikaObj.h"
|
||||
#include "dataQueue.h"
|
||||
#include "dataQueueObj.h"
|
||||
#include "dataStack.h"
|
||||
#include "dataStrs.h"
|
||||
#include "pikaCompiler.h"
|
||||
|
||||
/* const Pool output redirect */
|
||||
static void __handler_constPool_output_file(ConstPool* self, char* content) {
|
||||
/* to ram */
|
||||
uint16_t size = strGetSize(content) + 1;
|
||||
self->arg_buff = arg_append(self->arg_buff, content, size);
|
||||
/* to flash */
|
||||
__platform_fwrite(content, 1, size, self->output_f);
|
||||
}
|
||||
|
||||
/* instruct array output redirect */
|
||||
static void __handler_instructArray_output_none(InstructArray* self,
|
||||
InstructUnit* ins_unit) {
|
||||
/* none */
|
||||
}
|
||||
|
||||
static void __handler_instructArray_output_file(InstructArray* self,
|
||||
InstructUnit* ins_unit) {
|
||||
/* to flash */
|
||||
__platform_fwrite(ins_unit, 1, instructUnit_getSize(), self->output_f);
|
||||
}
|
||||
|
||||
/*
|
||||
need implament :
|
||||
__platform_fopen()
|
||||
__platform_fwrite()
|
||||
__platform_fclose()
|
||||
*/
|
||||
|
||||
int pikaCompile(char* output_file_name, char* py_lines) {
|
||||
ByteCodeFrame bytecode_frame = {0};
|
||||
|
||||
FILE* bytecode_f = __platform_fopen(output_file_name, "w+");
|
||||
/* main process */
|
||||
|
||||
/* step 1, get size of const pool and instruct array */
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_none;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
uint16_t const_pool_size = bytecode_frame.const_pool.size;
|
||||
uint16_t instruct_array_size = bytecode_frame.instruct_array.size;
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* step 2, write instruct array to file */
|
||||
__platform_fwrite(&instruct_array_size, 1, 2, bytecode_f);
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
/* instruct array to file */
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_file;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* step 3, write const pool to file */
|
||||
__platform_fwrite(&const_pool_size, 1, 2, bytecode_f);
|
||||
char void_ = 0;
|
||||
/* add \0 at the start */
|
||||
__platform_fwrite(&void_, 1, 1, bytecode_f);
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
/* const pool to file */
|
||||
bytecode_frame.const_pool.output_redirect_fun =
|
||||
__handler_constPool_output_file;
|
||||
/* instruct array to none */
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_none;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* deinit */
|
||||
__platform_fclose(bytecode_f);
|
||||
/* succeed */
|
||||
return 0;
|
||||
};
|
@ -0,0 +1,5 @@
|
||||
#ifndef __PIKA_COMPILER__H
|
||||
#define __PIKA_COMPILER__H
|
||||
|
||||
|
||||
#endif
|
@ -1200,11 +1200,13 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
|
||||
block_deepth_last = stack_getTop(block_stack);
|
||||
/* exit each block */
|
||||
for (int i = 0; i < block_deepth_last - block_deepth_now; i++) {
|
||||
QueueObj* exit_block_queue = obj_getObjWithKeepDeepth(ast, "exitBlock", 0);
|
||||
QueueObj* exit_block_queue =
|
||||
obj_getObjWithKeepDeepth(ast, "exitBlock", 0);
|
||||
/* create an exit_block queue */
|
||||
if (NULL == exit_block_queue) {
|
||||
obj_newObj(ast, "exitBlock", "", New_TinyObj);
|
||||
exit_block_queue = obj_getObjWithKeepDeepth(ast, "exitBlock", 0);
|
||||
exit_block_queue =
|
||||
obj_getObjWithKeepDeepth(ast, "exitBlock", 0);
|
||||
queueObj_init(exit_block_queue);
|
||||
}
|
||||
char buff[10] = {0};
|
||||
@ -1497,9 +1499,9 @@ static uint8_t Parser_checkIsMultiComment(char* line) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char* Parser_parsePyLines(Args* outBuffs,
|
||||
ByteCodeFrame* bytecode_frame,
|
||||
char* py_lines) {
|
||||
char* Parser_parsePyLines(Args* outBuffs,
|
||||
ByteCodeFrame* bytecode_frame,
|
||||
char* py_lines) {
|
||||
Stack block_stack;
|
||||
stack_init(&block_stack);
|
||||
Arg* asm_buff = arg_setStr(NULL, "", "");
|
||||
@ -1985,81 +1987,3 @@ void Parser_compilePyToBytecodeArray(char* lines) {
|
||||
/* deinit */
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
}
|
||||
|
||||
/* const Pool output redirect */
|
||||
static void __handler_constPool_output_file(ConstPool* self, char* content) {
|
||||
/* to ram */
|
||||
uint16_t size = strGetSize(content) + 1;
|
||||
self->arg_buff = arg_append(self->arg_buff, content, size);
|
||||
/* to flash */
|
||||
__platform_fwrite(content, 1, size, self->output_f);
|
||||
}
|
||||
|
||||
/* instruct array output redirect */
|
||||
static void __handler_instructArray_output_none(InstructArray* self,
|
||||
InstructUnit* ins_unit) {
|
||||
/* none */
|
||||
}
|
||||
|
||||
static void __handler_instructArray_output_file(InstructArray* self,
|
||||
InstructUnit* ins_unit) {
|
||||
/* to flash */
|
||||
__platform_fwrite(ins_unit, 1, instructUnit_getSize(), self->output_f);
|
||||
}
|
||||
|
||||
/*
|
||||
need implament :
|
||||
__platform_fopen()
|
||||
__platform_fwrite()
|
||||
__platform_fclose()
|
||||
*/
|
||||
int pikaCompile(char* output_file_name, char* py_lines) {
|
||||
ByteCodeFrame bytecode_frame = {0};
|
||||
|
||||
FILE* bytecode_f = __platform_fopen(output_file_name, "w+");
|
||||
/* main process */
|
||||
|
||||
/* step 1, get size of const pool and instruct array */
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_none;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
uint16_t const_pool_size = bytecode_frame.const_pool.size;
|
||||
uint16_t instruct_array_size = bytecode_frame.instruct_array.size;
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* step 2, write instruct array to file */
|
||||
__platform_fwrite(&instruct_array_size, 1, 2, bytecode_f);
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
/* instruct array to file */
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_file;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* step 3, write const pool to file */
|
||||
__platform_fwrite(&const_pool_size, 1, 2, bytecode_f);
|
||||
char void_ = 0;
|
||||
/* add \0 at the start */
|
||||
__platform_fwrite(&void_, 1, 1, bytecode_f);
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
/* const pool to file */
|
||||
bytecode_frame.const_pool.output_redirect_fun =
|
||||
__handler_constPool_output_file;
|
||||
/* instruct array to none */
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_none;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* deinit */
|
||||
__platform_fclose(bytecode_f);
|
||||
/* succeed */
|
||||
return 0;
|
||||
};
|
||||
|
@ -83,6 +83,9 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* bf, char* pikaAsm);
|
||||
int bytecodeFrame_fromMultiLine(ByteCodeFrame* bytecode_frame,
|
||||
char* python_lines);
|
||||
void Parser_compilePyToBytecodeArray(char* lines);
|
||||
char* Parser_parsePyLines(Args* outBuffs,
|
||||
ByteCodeFrame* bytecode_frame,
|
||||
char* py_lines);
|
||||
#define ParserState_forEach(parseState) \
|
||||
ParserState_beforeIter(&parseState); \
|
||||
for (int i = 0; i < parseState.length; i++)
|
||||
|
87
src/pikaCompiler.c
Normal file
87
src/pikaCompiler.c
Normal file
@ -0,0 +1,87 @@
|
||||
#include "PikaParser.h"
|
||||
#include "BaseObj.h"
|
||||
#include "PikaObj.h"
|
||||
#include "dataQueue.h"
|
||||
#include "dataQueueObj.h"
|
||||
#include "dataStack.h"
|
||||
#include "dataStrs.h"
|
||||
#include "pikaCompiler.h"
|
||||
|
||||
/* const Pool output redirect */
|
||||
static void __handler_constPool_output_file(ConstPool* self, char* content) {
|
||||
/* to ram */
|
||||
uint16_t size = strGetSize(content) + 1;
|
||||
self->arg_buff = arg_append(self->arg_buff, content, size);
|
||||
/* to flash */
|
||||
__platform_fwrite(content, 1, size, self->output_f);
|
||||
}
|
||||
|
||||
/* instruct array output redirect */
|
||||
static void __handler_instructArray_output_none(InstructArray* self,
|
||||
InstructUnit* ins_unit) {
|
||||
/* none */
|
||||
}
|
||||
|
||||
static void __handler_instructArray_output_file(InstructArray* self,
|
||||
InstructUnit* ins_unit) {
|
||||
/* to flash */
|
||||
__platform_fwrite(ins_unit, 1, instructUnit_getSize(), self->output_f);
|
||||
}
|
||||
|
||||
/*
|
||||
need implament :
|
||||
__platform_fopen()
|
||||
__platform_fwrite()
|
||||
__platform_fclose()
|
||||
*/
|
||||
|
||||
int pikaCompile(char* output_file_name, char* py_lines) {
|
||||
ByteCodeFrame bytecode_frame = {0};
|
||||
|
||||
FILE* bytecode_f = __platform_fopen(output_file_name, "w+");
|
||||
/* main process */
|
||||
|
||||
/* step 1, get size of const pool and instruct array */
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_none;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
uint16_t const_pool_size = bytecode_frame.const_pool.size;
|
||||
uint16_t instruct_array_size = bytecode_frame.instruct_array.size;
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* step 2, write instruct array to file */
|
||||
__platform_fwrite(&instruct_array_size, 1, 2, bytecode_f);
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
/* instruct array to file */
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_file;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* step 3, write const pool to file */
|
||||
__platform_fwrite(&const_pool_size, 1, 2, bytecode_f);
|
||||
char void_ = 0;
|
||||
/* add \0 at the start */
|
||||
__platform_fwrite(&void_, 1, 1, bytecode_f);
|
||||
byteCodeFrame_init(&bytecode_frame);
|
||||
bytecode_frame.const_pool.output_f = bytecode_f;
|
||||
bytecode_frame.instruct_array.output_f = bytecode_f;
|
||||
/* const pool to file */
|
||||
bytecode_frame.const_pool.output_redirect_fun =
|
||||
__handler_constPool_output_file;
|
||||
/* instruct array to none */
|
||||
bytecode_frame.instruct_array.output_redirect_fun =
|
||||
__handler_instructArray_output_none;
|
||||
Parser_parsePyLines(NULL, &bytecode_frame, py_lines);
|
||||
byteCodeFrame_deinit(&bytecode_frame);
|
||||
|
||||
/* deinit */
|
||||
__platform_fclose(bytecode_f);
|
||||
/* succeed */
|
||||
return 0;
|
||||
};
|
5
src/pikaCompiler.h
Normal file
5
src/pikaCompiler.h
Normal file
@ -0,0 +1,5 @@
|
||||
#ifndef __PIKA_COMPILER__H
|
||||
#define __PIKA_COMPILER__H
|
||||
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user