Skip to content

Commit

Permalink
Ternary expresisons parsing fully implemented (#54 from wesuRage/main)
Browse files Browse the repository at this point in the history
Ternary expresisons parsing fully implemented
  • Loading branch information
wesuRage authored Dec 12, 2024
2 parents 1287551 + 0b7c9bf commit d8b9f71
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 83 deletions.
36 changes: 32 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,45 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)

# Define the main executable from the main.c file
add_executable(galaxy src/main.c)
target_include_directories(galaxy PRIVATE include)
include_directories(${PROJECT_SOURCE_DIR}/include)

# Link libraries to galaxy
target_link_libraries(galaxy PRIVATE arg_parse lexer parser node_definitions generator)

# Add subdirectories for lexer, parser, and node_definitions
add_subdirectory(src/args)
add_subdirectory(src/frontend/lexer)
add_subdirectory(src/frontend/parser)
add_subdirectory(src/frontend/node_definitions)
add_subdirectory(src/backend/generator)

# Locate LLVM using llvm-config
find_package(LLVM REQUIRED CONFIG)

# Retrieve LLVM flags
execute_process(COMMAND llvm-config --cxxflags OUTPUT_VARIABLE CMAKE_CXX_FLAGS)
execute_process(COMMAND llvm-config --libs OUTPUT_VARIABLE LLVM_LIBS)
execute_process(COMMAND llvm-config --system-libs OUTPUT_VARIABLE LLVM_SYS_LIBS)
execute_process(COMMAND llvm-config --ldflags OUTPUT_VARIABLE LLVM_LDFLAGS)

# Strip trailing spaces and newlines
string(STRIP "${LLVM_LIBS}" LLVM_LIBS)
string(STRIP "${LLVM_SYS_LIBS}" LLVM_SYS_LIBS)
string(STRIP "${LLVM_LDFLAGS}" LLVM_LDFLAGS)
string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")

# Include LLVM directories
include_directories(${LLVM_INCLUDE_DIRS})
link_directories(${LLVM_LIBRARY_DIRS})
add_definitions(${LLVM_DEFINITIONS})

# Link LLVM libraries
link_libraries(${LLVM_LIBS} ${LLVM_SYS_LIBS} ${LLVM_LDFLAGS})

# Create a test executable if the test file exists
if(EXISTS "src/main.cpp")
add_executable(galaxy src/main.cpp)
target_include_directories(galaxy PRIVATE ${PROJECT_SOURCE_DIR}/include ${LLVM_INCLUDE_DIRS})
target_compile_options(galaxy PRIVATE ${CMAKE_CXX_FLAGS})
target_link_libraries(galaxy PRIVATE arg_parse lexer parser node_definitions generator ${LLVM_LIBS} ${LLVM_SYS_LIBS})
endif()
8 changes: 1 addition & 7 deletions examples/a.glx
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
def my_return( int i) -> int:
return i;
end;

def main( ) -> int:
return my_return( 10) ;
end;
bool isBigger := 3 > 2 ? 1 : 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef PRINT_TERNARY_H
#define PRINT_TERNARY_H

#include "frontend/ast/definitions.h"
#include "frontend/parser/printer/visited.h"

void print_ternary(const AstNode *node, int depth, VisitedNodes *visited);

#endif // PRINT_TERNARY_H
1 change: 1 addition & 0 deletions src/frontend/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ TokenType match_keyword(const char *lexeme) {
if (strcmp(lexeme, "true") == 0) return TOKEN_TRUE;
if (strcmp(lexeme, "false") == 0) return TOKEN_FALSE;
if (strcmp(lexeme, "const") == 0) return TOKEN_CONST;
if (strcmp(lexeme, "bool") == 0) return TOKEN_TYPE_BOOL;
if (strcmp(lexeme, "int") == 0) return TOKEN_TYPE_INT;
if (strcmp(lexeme, "float") == 0) return TOKEN_TYPE_FLOAT;
if (strcmp(lexeme, "double") == 0) return TOKEN_TYPE_DOUBLE;
Expand Down
10 changes: 8 additions & 2 deletions src/frontend/parser/expressions/parse_assignment_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@
#include "frontend/ast/definitions.h"
#include "frontend/parser/expressions/parse_assignment_expr.h"
#include "frontend/parser/expressions/parse_expr.h"
#include "frontend/parser/expressions/parse_ternary_expr.h"
#include "frontend/parser/expressions/parse_object_expr.h"

AstNode *parse_assignment_expr(Parser *parser) {
int line = at(parser).line;
int column_start = at(parser).column_start;
int position_start = at(parser).position_start;

AstNode *left = parse_object_expr(parser);
AstNode *left = parse_ternary_expr(parser);

if (at(parser).type == TOKEN_ASSIGN) {
eat(parser);

AstNode *value = parse_expr(parser);
AstNode *value;

switch (at(parser).type) {
case TOKEN_OBRACE: value = parse_object_expr(parser);
default: value = parse_ternary_expr(parser);
}

int column_end = at(parser).column_end - 1;
int position_end = at(parser).position_end - 1;
Expand Down
18 changes: 16 additions & 2 deletions src/frontend/parser/expressions/parse_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "frontend/parser/expressions/parse_expr.h"
#include "frontend/parser/expressions/parse_unary_expr.h"
#include "frontend/parser/expressions/parse_assignment_expr.h"
#include "frontend/parser/expressions/binary_operations/parse_additive_expr.h"
#include "frontend/parser/expressions/binary_operations/parse_bitwise_expr.h"

AstNode *parse_expr(Parser *parser) {
if (
Expand All @@ -15,7 +15,21 @@ AstNode *parse_expr(Parser *parser) {
|| at(parser).type == TOKEN_BITWISE_NOT
|| at(parser).type == TOKEN_INCREMENT
|| at(parser).type == TOKEN_DECREMENT
|| next(parser).type == TOKEN_INCREMENT
|| next(parser).type == TOKEN_DECREMENT
) return parse_unary_expr(parser);

return parse_assignment_expr(parser);
switch (next(parser).type){
case TOKEN_BITWISE_AND:
case TOKEN_BITWISE_OR:
case TOKEN_BITWISE_XOR:
case TOKEN_SHIFT_LEFT:
case TOKEN_SHIFT_RIGHT:
case TOKEN_PLUS:
case TOKEN_MINUS:
case TOKEN_MUL:
case TOKEN_DIV:
case TOKEN_MODULUS: return parse_bitwise_expr(parser);
default: return parse_assignment_expr(parser);
}
}
21 changes: 21 additions & 0 deletions src/frontend/parser/printer/nodes/expressions/print_ternary.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "frontend/parser/printer/nodes/expressions/print_ternary.h"
#include "frontend/ast/definitions.h"
#include "frontend/parser/printer/print_indent.h"
#include "frontend/parser/printer/print_ast.h"
#include "frontend/parser/printer/visited.h"

void print_ternary(const AstNode *node, int depth, VisitedNodes *visited){
TernaryNode *ternary_data = (TernaryNode *)node->data;

print_indent(depth + 1);
printf("Condition:\n");
print_ast_node(ternary_data->condition, depth + 2, visited);

print_indent(depth + 1);
printf("Consequent:\n");
print_ast_node(ternary_data->consequent, depth + 2, visited);

print_indent(depth + 1);
printf("Alternate:\n");
print_ast_node(ternary_data->alternate, depth + 2, visited);
}
5 changes: 5 additions & 0 deletions src/frontend/parser/printer/print_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "frontend/parser/printer/nodes/expressions/print_boolean.h"
#include "frontend/parser/printer/nodes/expressions/print_call.h"
#include "frontend/parser/printer/nodes/expressions/print_return.h"
#include "frontend/parser/printer/nodes/expressions/print_ternary.h"

#include "frontend/parser/printer/nodes/statements/print_import.h"
#include "frontend/parser/printer/nodes/statements/print_package.h"
Expand Down Expand Up @@ -113,6 +114,10 @@ void print_ast_node(const AstNode *node, int depth, VisitedNodes *visited) {
print_boolean(node, depth);
} break;

case NODE_TERNARY: {
print_ternary(node, depth, visited);
} break;

case NODE_RETURN: {
print_return(node, depth, visited);
} break;
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/parser/printer/print_indent.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
*/
void print_indent(int depth) {
for (int i = 0; i < depth; i++) {
printf(" ");
printf(" ");
}
}
67 changes: 0 additions & 67 deletions src/main.c

This file was deleted.

Loading

0 comments on commit d8b9f71

Please sign in to comment.