This commit is contained in:
SpookyDervish
2025-12-27 10:17:56 +11:00
parent bd07952569
commit 9da2bb5404
13 changed files with 598 additions and 57 deletions

1
src/sylt/ast.c Normal file
View File

@@ -0,0 +1 @@
#include "ast.h"

65
src/sylt/ast.h Normal file
View File

@@ -0,0 +1,65 @@
#ifndef AST_H
#define AST_H
#include <stdlib.h>
typedef enum {
NODE_PROGRAM,
// Statements
NODE_EXPRESSION_STATEMENT,
// Expressions
NODE_INFIX_EXPRESSION,
// Literals
NODE_INT_LITERAL,
NODE_FLOAT_LITERAL
} AST_Type;
typedef struct AST AST;
struct AST
{
AST_Type type;
char *(*to_json)(AST *node);
};
typedef struct
{
AST base;
AST **statements;
size_t statementCount;
} Program;
char *programToJSON(AST *node);
// statements
typedef struct
{
AST base;
AST *expr;
} ExpressionStatement;
char *expressionStatementToJSON(AST *node);
// expressions
typedef struct {
AST base;
AST *left;
char *operator;
AST *right;
} InfixExpression;
char *infixExpressionToJSON(AST *node);
// literals
typedef struct {
AST base;
int value;
} IntegerLiteral;
char *integerLiteralToJSON(AST *node);
typedef struct {
AST base;
double value;
} FloatLiteral;
char *floatLiteralToJSON(AST *node);
#endif // !AST_H

21
src/sylt/exception.c Normal file
View File

@@ -0,0 +1,21 @@
#include "exception.h"
#include <stdio.h>
#include "../utils/ansii.h"
char *exceptionTypeToCString(ExceptionType type) {
switch (type)
{
case EXCEPTION_MALFORMED_NUMBER:
return "MalformedNumberException";
case EXCEPTION_MEMORY_ALLOCATION_FAILURE:
return "MemoryAllocationFailException";
default:
return "UnkownException (fix me please)";
}
}
void exceptionMessage(ExceptionType type, char *message, int lineNumber, int position) {
printf(ESC_RED_FG ESC_BOLD "%s: %s" ESC_RESET "\n", exceptionTypeToCString(type), message);
}

12
src/sylt/exception.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef EXCEPTION_H
#define EXCEPTION_H
typedef enum {
EXCEPTION_MALFORMED_NUMBER,
EXCEPTION_MEMORY_ALLOCATION_FAILURE
} ExceptionType;
char *exceptionTypeToCString(ExceptionType type);
void exceptionMessage(ExceptionType type, char *message, int lineNumber, int position);
#endif // !EXCEPTION_H

1
src/sylt/parser.c Normal file
View File

@@ -0,0 +1 @@
#include "parser.h"

28
src/sylt/parser.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef PARSER_H
#define PARSER_H
#include "lexer.h"
#include "ast.h"
typedef enum {
PRECEDENCE_LOWEST = 0,
PRECEDENCE_EQUALS,
PRECEDENCE_LESSGREATER,
PRECEDENCE_SUM,
PRECEDENCE_PRODUCT,
PRECEDENCE_EXPONENT,
PRECEDENCE_PREFIX,
PRECEDENCE_CALL,
PRECEDENCE_INDEX,
} PrecedenceType;
PrecedenceType PRECEDENCES[] = {
[TOKEN_PLUS] = PRECEDENCE_SUM,
[TOKEN_MINUS] = PRECEDENCE_SUM,
[TOKEN_SLASH] = PRECEDENCE_PRODUCT,
[TOKEN_ASTERISK] = PRECEDENCE_PRODUCT,
[TOKEN_MODULUS] = PRECEDENCE_PRODUCT,
[TOKEN_POW] = PRECEDENCE_EXPONENT,
};
#endif // !PARSER_H

45
src/sylt/token.c Normal file
View File

@@ -0,0 +1,45 @@
#include "token.h"
char *tokenTypeToCString(TokenType type) {
switch (type)
{
case TOKEN_EOF:
return "TOKEN_EOF";
case TOKEN_ILLEGAL:
return "TOKEN_ILLEGAL";
case TOKEN_INT:
return "TOKEN_INT";
case TOKEN_FLOAT:
return "TOKEN_FLOAT";
case TOKEN_PLUS:
return "TOKEN_EOF";
case TOKEN_MINUS:
return "TOKEN_MINUS";
case TOKEN_ASTERISK:
return "TOKEN_ASTERISK";
case TOKEN_SLASH:
return "TOKEN_SLASH";
case TOKEN_POW:
return "TOKEN_POW";
case TOKEN_MODULUS:
return "TOKEN_MODULUS";
case TOKEN_LPAREN:
return "TOKEN_LPAREN";
case TOKEN_RPAREN:
return "TOKEN_RPAREN";
default:
return "UNKOWN (fix me please)";
}
}