diff --git a/README.md b/README.md index 8a300fc..5965386 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ But uhh that's the name of the VM itself, the name of the programming language I made is Sylt, named after the German island. To compile the VM for Linux: `gcc src/main.c src/vmbl.c src/exception.c src/file_utils.c -o vmbl -O3` +To compile the SYLT compiler for Linux: `gcc src/sylt/main.c src/file_utils.c src/sylt/lexer.c -o sylt -O3` SASM and Sylt are written in Python for now as I'm too mentally challenged to write C code rn. diff --git a/fib.vmbl b/fib.vmbl new file mode 100644 index 0000000..a48e1ff Binary files /dev/null and b/fib.vmbl differ diff --git a/src/sylt/lexer.c b/src/sylt/lexer.c new file mode 100644 index 0000000..84a7c09 --- /dev/null +++ b/src/sylt/lexer.c @@ -0,0 +1,87 @@ +#include "lexer.h" +#include +#include +#include + +Lexer initLexer(char *source) { + Lexer newLexer = { + .source = source, + .position = -1, + .readPosition = 0, + .lineNumber = 1, + .currentChar = '\0' + }; + return newLexer; +} + +void readChar(Lexer *lexer) { + if (lexer->readPosition >= strlen(lexer->source)) + lexer->currentChar = '\0'; + else + lexer->currentChar = lexer->source[lexer->readPosition]; + + lexer->position = lexer->readPosition; + lexer->readPosition++; +} + +void skipWhitespace(Lexer *lexer) { + switch (lexer->currentChar) + { + case ' ': + case '\t': + case '\n': + case '\r': + if (lexer->currentChar == '\n') + lexer->lineNumber++; + + readChar(lexer); + break; + + default: + break; + } + +} + +Token nextToken(Lexer *lexer) { + //Token tok; + skipWhitespace(lexer); + + switch (lexer->currentChar) + { + case '+': + return NEW_TOKEN(lexer, PLUS, "+"); + break; + case '-': + return NEW_TOKEN(lexer, MINUS, "-"); + break; + case '*': + return NEW_TOKEN(lexer, ASTERISK, "*"); + break; + case '/': + return NEW_TOKEN(lexer, SLASH, "/"); + break; + case '%': + return NEW_TOKEN(lexer, MODULUS, "%"); + break; + case '^': + return NEW_TOKEN(lexer, POW, "^"); + break; + case '(': + return NEW_TOKEN(lexer, LPAREN, "("); + break; + case ')': + return NEW_TOKEN(lexer, RPAREN, ")"); + break; + case '\0': + return NEW_TOKEN(lexer, EOF, ""); + break; + + default: + if (isdigit()) + + break; + } + + //return tok; +} \ No newline at end of file diff --git a/src/sylt/lexer.h b/src/sylt/lexer.h new file mode 100644 index 0000000..e7e986d --- /dev/null +++ b/src/sylt/lexer.h @@ -0,0 +1,22 @@ +#ifndef LEXER_H +#define LEXER_H +#include "token.h" +#include + +typedef struct +{ + int position; + int readPosition; + int lineNumber; + + char currentChar; + char *source; +} Lexer; + +Lexer initLexer(char *source); +void readChar(Lexer *lexer); +void skipWhitespace(Lexer *lexer); +Token nextToken(Lexer *lexer); +char *tokenToCStr(Token token); + +#endif // !LEXER_H diff --git a/src/sylt/main.c b/src/sylt/main.c new file mode 100644 index 0000000..0a76d32 --- /dev/null +++ b/src/sylt/main.c @@ -0,0 +1,10 @@ +#include "lexer.h" +#include +#include "../file_utils.h" + +int main() { + Lexer lexer = initLexer("const x, y = 1, 3\n"); + printf(nextToken(&lexer).literal); + + return 0; +} \ No newline at end of file diff --git a/src/sylt/token.h b/src/sylt/token.h new file mode 100644 index 0000000..85e7cd5 --- /dev/null +++ b/src/sylt/token.h @@ -0,0 +1,36 @@ +#ifndef TOKEN_H +#define TOKEN_H + +typedef enum { + // Special Tokens + EOF, + ILLEGAL, + + // Data Types + INT, + FLOAT, + + // Arithmetic Symbols + PLUS, + MINUS, + ASTERISK, + SLASH, + POW, + MODULUS, + + // Symbols + LPAREN, + RPAREN +} TokenType; + +typedef struct +{ + TokenType type; + char *literal; + int lineNumber; + int position; +} Token; + +#define NEW_TOKEN(lexer, tokType, tokLiteral) (Token){ .type = (tokType), .literal = (tokLiteral), .lineNumber = (lexer->lineNumber), .position = (lexer->position) } + +#endif // !TOKEN_H \ No newline at end of file diff --git a/sylt b/sylt index 81ba3da..71c2ca5 100644 Binary files a/sylt and b/sylt differ