+++ /dev/null
-/**
- @file ast.c
- @brief See header for details
- $Revision$
- $HeadURL$
-*/
-#include "ast.h"
-
-void ast_free(void* p_obj)
-{
- (void)p_obj;
-}
-
-ast_t* ast_new(ast_type_t type, void* value)
-{
- (void)type;
- (void)value;
- return NULL;
-}
-
-void ast_set_pos(ast_t* p_ast, const char* file, size_t line, size_t col)
-{
- (void)p_ast;
- (void)file;
- (void)line;
- (void)col;
-}
-
-const pos_t* ast_get_pos(ast_t* p_ast)
-{
- (void)p_ast;
- return NULL;
-}
-
-void ast_set_type(ast_t* p_ast, ast_type_t type)
-{
- (void)p_ast;
- (void)type;
-}
-
-ast_type_t ast_get_type(ast_t* p_ast)
-{
- (void)p_ast;
- return UNKNOWN;
-}
-
-void ast_set_value(ast_t* p_ast, void* value)
-{
- (void)p_ast;
- (void)value;
-}
-
-const void* ast_get_value(ast_t* p_ast)
-{
- (void)p_ast;
- return NULL;
-}
-
-void ast_set_children(ast_t* p_ast, child_t* p_children)
-{
- (void)p_ast;
- (void)p_children;
-}
-
-const child_t* ast_get_children(ast_t* p_ast)
-{
- (void)p_ast;
- return NULL;
-}
-
-ast_t* ast_map(const ast_t* p_ast, ast_map_fn_t p_fn)
-{
- (void)p_ast;
- (void)p_fn;
- return NULL;
-}
+++ /dev/null
-/**
- @file ast.h
- @brief TODO: Describe this file
- $Revision$
- $HeadURL$
- */
-#ifndef AST_H
-#define AST_H
-
-#include <stddef.h>
-#include <string.h>
-
-typedef enum {
- BOOLEAN,
- INTEGER,
- CHARACTER,
- STRING,
- UNKNOWN
-} ast_type_t;
-
-typedef struct {
- char* file;
- size_t line;
- size_t column;
-} pos_t;
-
-struct ast_t;
-
-typedef struct child_t {
- struct ast_t* ast;
- struct child_t* next;
-} child_t;
-
-typedef struct ast_t {
- pos_t* pos;
- ast_type_t type;
- void* value;
- child_t* children;
-} ast_t;
-
-typedef ast_t* (*ast_map_fn_t)(const ast_t* p_ast);
-
-void ast_free(void* p_obj);
-
-ast_t* ast_new(ast_type_t type, void* value);
-
-void ast_set_pos(ast_t* p_ast, const char* file, size_t line, size_t col);
-
-const pos_t* ast_get_pos(ast_t* p_ast);
-
-void ast_set_type(ast_t* p_ast, ast_type_t type);
-
-ast_type_t ast_get_type(ast_t* p_ast);
-
-void ast_set_value(ast_t* p_ast, void* value);
-
-const void* ast_get_value(ast_t* p_ast);
-
-void ast_set_children(ast_t* p_ast, child_t* p_children);
-
-const child_t* ast_get_children(ast_t* p_ast);
-
-ast_t* ast_map(const ast_t* p_ast, ast_map_fn_t p_fn);
-
-#endif /* AST_H */
--- /dev/null
+#include "parser.h"
+#include "codegen.h"
+
+static tree_t* get_child(tree_t* p_tree, size_t idx) {
+ tree_t* child = NULL;
+ if (p_tree->tag == TREE) {
+ vec_t* vec = p_tree->ptr.vec;
+ if (idx < vec_size(vec))
+ child = vec_at(vec, idx);
+ }
+ return child;
+}
+
+static void* get_val(tree_t* p_tree) {
+ void* ret = NULL;
+ if (p_tree->tag == ATOM) {
+ ret = p_tree->ptr.tok->value;
+ }
+ return ret;
+}
+
+static void* get_child_val(tree_t* p_tree, size_t idx) {
+ void* ret = NULL;
+ tree_t* child = get_child(p_tree,idx);
+ if (child != NULL) {
+ ret = get_val(child);
+ }
+ return ret;
+}
+
+static bool is_formtype(tree_t* p_tree, const char* val) {
+ bool ret = false;
+ tree_t* child = get_child(p_tree, 0);
+ if (child->tag == ATOM) {
+ lex_tok_t* token = child->ptr.tok;
+ if ((token->type == T_VAR) &&
+ (0 == strcmp(val, (char*)token->value))) {
+ ret = true;
+ }
+ }
+ return ret;
+}
+
+/*****************************************************************************/
+
+static void emit_header(void) {
+ puts("#include <sclpl.h>\n");
+}
+
+static void emit_def_placeholders(vec_t* prgrm) {
+ for (size_t idx = 0; idx < vec_size(prgrm); idx++) {
+ tree_t* p_tree = (tree_t*)vec_at(prgrm, idx);
+ if (is_formtype(p_tree, "def")) {
+ tree_t* child = get_child(p_tree, 2);
+ //printf("%p\n", child);
+ //if (is_formtype(get_child(p_tree, 2), "fn")) {
+ //printf("val %s();\n", (char*)get_child_val(p_tree,1));
+ //} else {
+ printf("val %s;\n", (char*)get_child_val(p_tree,1));
+ //}
+ }
+ }
+ puts("");
+}
+
+static void emit_footer(void) {
+
+}
+
+void codegen_csource(FILE* file, vec_t* program) {
+ emit_header();
+ emit_def_placeholders(program);
+ emit_footer();
+}
--- /dev/null
+/**
+ @file codegen.h
+ @brief TODO: Describe this file
+ $Revision$
+ $HeadURL$
+ */
+#ifndef CODEGEN_H
+#define CODEGEN_H
+
+#include <stdio.h>
+#include "vec.h"
+
+void codegen_csource(FILE* file, vec_t* program);
+
+#endif /* CODEGEN_H */
opts_cfg_t Options_Config[] = {
{"tokens", false, "mode", "Emit the token output of lexical analysis for the given file"},
{"ast", false, "mode", "Emit the abstract syntax tree for the given file"},
+ {"csource", false, "mode", "Emit the intermediate C source file for the given file"},
{"repl", false, "mode", "Execute the application in a REPL"},
{"staticlib", false, "mode", "Compile the application as a static library"},
{"sharedlib", false, "mode", "Compile the application as a shared library"},
return ret;
}
+static int emit_csource(void) {
+ int ret = 0;
+ parser_t* p_parser = parser_new(NULL, stdin);
+ vec_t* p_vec = vec_new(0);
+ while(!parser_eof(p_parser)) {
+ tree_t* p_tree = grammar_toplevel(p_parser);
+ if (NULL != p_tree) {
+ tree_t* p_ast = convert_to_ast(p_tree);
+ mem_release(p_tree);
+ vec_push_back(p_vec, p_ast);
+ } else {
+ parser_resume(p_parser);
+ ret = 1;
+ }
+ }
+ codegen_csource(stdout, p_vec);
+ mem_release(p_vec);
+ mem_release(p_parser);
+ return ret;
+}
+
static int exec_repl(void) {
parser_t* p_parser = parser_new(":> ", stdin);
while(!parser_eof(p_parser)) {
return emit_tokens();
} else if (opts_equal(NULL, "mode", "ast")) {
return emit_tree();
+ } else if (opts_equal(NULL, "mode", "csource")) {
+ return emit_csource();
} else if (opts_equal(NULL, "mode", "staticlib")) {
return emit_staticlib();
} else if (opts_equal(NULL, "mode", "sharedlib")) {