ls

simple syscall based programming language from scratch
git clone git://git.kocotian.pl/ls.git
Log | Files | Refs | README

commit a71615b043d700b59c1e88b511911bf2340f18e6
parent c22cc3608a9459db36d5b71a93613da05cf30b30
Author: kocotian <kocotian@kocotian.pl>
Date:   Fri, 26 Feb 2021 22:05:51 +0100

functions generation working! + tokens debugging to stderr & few changes

Diffstat:
Mgrammar.c | 21++++++++++++++++++---
Mlsc.c | 13+++++++------
2 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/grammar.c b/grammar.c @@ -3,8 +3,9 @@ #include "grammar.h" -extern char *contents; -extern char *output; +extern char *contents; +extern char *output; +extern size_t outsiz; static char * g_typetostr(TokenType type) @@ -104,9 +105,13 @@ g_statement(Token *tokens, size_t toksize) size_t g_function(Token *tokens, size_t toksize) { - size_t i; + char *func_name; + char buffer[BUFSIZ]; + + size_t i, rb; i = 0; g_expecttype(tokens[i++], TokenIdentifier); + func_name = strndup(contents + tokens[i - 1].off, tokens[i - 1].len); g_expecttype(tokens[i], TokenOpeningParenthesis); if (tokens[i + 1].type == TokenKeyword && !strncmp(contents + tokens[i + 1].off, "void", 4)) @@ -116,8 +121,18 @@ g_function(Token *tokens, size_t toksize) g_expecttype(tokens[i++], TokenIdentifier); } while (tokens[i].type == TokenComma); g_expecttype(tokens[i++], TokenClosingParenthesis); + + rb = snprintf(buffer, BUFSIZ, "%s:\n", func_name); + output = realloc(output, outsiz += rb); + strcat(output, buffer); + free(func_name); + i += g_statement(tokens + i, toksize - i); + rb = snprintf(buffer, BUFSIZ, "\tret\n"); + output = realloc(output, outsiz += rb); + strcat(output, buffer); + return ++i; } diff --git a/lsc.c b/lsc.c @@ -166,22 +166,23 @@ main(int argc, char *argv[]) const char space = ' '; int j; for (j = 0; j < tokiter; ++j) { - write(1, contents + tokens[j].off, tokens[j].len); - write(1, &space, 1); + write(2, contents + tokens[j].off, tokens[j].len); + write(2, &space, 1); } - write(1, "\n", 1); + write(2, "\n", 1); } output = malloc(outsiz = snprintf(buffer, BUFSIZ, "BITS 64\n" - "section .text\nglobal _start\n_start:\n\t" - "call main\n\tmov rax, 60\n\tmov rdi, 0\n\tsyscall\n") + 1); + "section .text\nglobal _start\n_start:\n" + "\tcall main\n\tmov rax, 60\n\tmov rdi, 0\n" + "\tsyscall\n\tret\n") + 1); memcpy(output, buffer, outsiz); memcpy(output + outsiz, "", 1); g_main(tokens, tokiter); - write(1, output, outsiz); + write(1, output, outsiz - 1); free(output); free(tokens);