slowly writing a lexer, literally not functional lmao
This commit is contained in:
@@ -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.
|
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 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.
|
SASM and Sylt are written in Python for now as I'm too mentally challenged to write C code rn.
|
||||||
|
|
||||||
|
|||||||
87
src/sylt/lexer.c
Normal file
87
src/sylt/lexer.c
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
#include "lexer.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio,h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
22
src/sylt/lexer.h
Normal file
22
src/sylt/lexer.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef LEXER_H
|
||||||
|
#define LEXER_H
|
||||||
|
#include "token.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
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
|
||||||
10
src/sylt/main.c
Normal file
10
src/sylt/main.c
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include "lexer.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "../file_utils.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Lexer lexer = initLexer("const x, y = 1, 3\n");
|
||||||
|
printf(nextToken(&lexer).literal);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
36
src/sylt/token.h
Normal file
36
src/sylt/token.h
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user