Skip to content

Commit 10c74bf

Browse files
author
TheDevConnor
committed
Started making a chess engine and made a make file for the chess program. I will make one for all the test file soon
1 parent fbae450 commit 10c74bf

10 files changed

Lines changed: 585 additions & 15 deletions

File tree

LPBS/ast.lx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ pub const Group -> struct {
5757
};
5858

5959
#returns_ownership
60-
pub const create_program_node -> fn (nodes: *Expr) *Stmt {
61-
let node: *Program = cast<*Program>(alloc(sizeof<Program>));
60+
pub const create_program_node -> fn (nodes: *Expr, size: int) *Stmt {
61+
let node: *Program = cast<*Program>(alloc(size * sizeof<Program>));
6262
let base_node: *Stmt = cast<*Stmt>(node);
6363

6464
base_node.type = StmtKind::STMT_PROGRAM;
@@ -134,7 +134,7 @@ pub const free_expr -> fn (node: *Expr) void {
134134
if (node == cast<*Expr>(0)) { return; }
135135

136136
switch(node.type) {
137-
ExprKind::EXPR_NUMBER, ExprKind::EXPR_IDENT, ExprKind::EXPR_STRING -> {}
137+
ExprKind::EXPR_NUMBER, ExprKind::EXPR_IDENT, ExprKind::EXPR_STRING -> { /* No child nodes to free */ }
138138
ExprKind::EXPR_BINARY -> {
139139
let bin_node: *Binary = cast<*Binary>(node);
140140
free_expr(bin_node.left); free_expr(bin_node.right);

LPBS/main.lx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pub const main -> fn () int {
1111
let speed: Timer = time::timer_start();
1212

13-
let path: *char = "/home/connor/Projects/Luma/LPBS/build.lmb";
13+
let path: *char = "/home/thedevconnor/Projects/Luma/LPBS/build.lmb";
1414
let file: *char = io::read_file(path);
1515
let lex: *Token = lexer::scan(file);
1616

LPBS/parser.lx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,5 @@ pub const parse -> fn (tks: *Token, path: *char) *Stmt {
147147

148148
let node: *Expr = parse_expr(&psr, BindingPower::BP_NONE);
149149

150-
return ast::create_program_node(node);
150+
return ast::create_program_node(node, tks.size);
151151
}

src/llvm/expr.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,28 @@ LLVMValueRef codegen_expr_literal(CodeGenContext *ctx, AstNode *node) {
8888
case LITERAL_INT:
8989
return LLVMConstInt(LLVMInt64TypeInContext(ctx->context),
9090
node->expr.literal.value.int_val, false);
91-
91+
9292
case LITERAL_FLOAT:
9393
return LLVMConstReal(LLVMDoubleTypeInContext(ctx->context),
9494
node->expr.literal.value.float_val);
9595

9696
case LITERAL_BOOL:
9797
return LLVMConstInt(LLVMInt1TypeInContext(ctx->context),
9898
node->expr.literal.value.bool_val ? 1 : 0, false);
99-
99+
100100
case LITERAL_CHAR:
101101
return LLVMConstInt(LLVMInt8TypeInContext(ctx->context),
102102
(unsigned char)node->expr.literal.value.char_val,
103103
false);
104-
104+
105105
case LITERAL_STRING: {
106106
char *processed_str =
107107
process_escape_sequences(node->expr.literal.value.string_val);
108-
108+
109109
// String literals must be created in the current module
110110
LLVMModuleRef current_llvm_module =
111111
ctx->current_module ? ctx->current_module->module : ctx->module;
112-
112+
113113
// Create the global string in the current module
114114
LLVMValueRef global_str =
115115
LLVMAddGlobal(current_llvm_module,
@@ -145,9 +145,10 @@ LLVMValueRef codegen_expr_literal(CodeGenContext *ctx, AstNode *node) {
145145
case LITERAL_NULL:
146146
return LLVMConstNull(
147147
LLVMPointerType(LLVMInt8TypeInContext(ctx->context), 0));
148-
148+
149149
default:
150-
fprintf(stderr, "ERROR: Unknown literal type: %d\n", node->expr.literal.lit_type);
150+
fprintf(stderr, "ERROR: Unknown literal type: %d\n",
151+
node->expr.literal.lit_type);
151152
return NULL;
152153
}
153154
}
@@ -1063,7 +1064,14 @@ LLVMValueRef codegen_expr_index(CodeGenContext *ctx, AstNode *node) {
10631064

10641065
// If this is the last field in the chain, get its element type
10651066
if (i == chain_length - 1) {
1066-
element_type = current_struct->field_element_types[field_idx];
1067+
LLVMTypeRef field_type = current_struct->field_types[field_idx];
1068+
1069+
// Check if this field is an array - if so, get its element type
1070+
if (LLVMGetTypeKind(field_type) == LLVMArrayTypeKind) {
1071+
element_type = LLVMGetElementType(field_type);
1072+
} else {
1073+
element_type = current_struct->field_element_types[field_idx];
1074+
}
10671075
break;
10681076
}
10691077

src/llvm/struct.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ LLVMValueRef codegen_expr_struct_access(CodeGenContext *ctx, AstNode *node) {
479479
}
480480

481481
} else if (object->type == AST_EXPR_MEMBER) {
482-
// **NEW: Handle chained member access like psr.tks.list**
482+
// **Handle chained member access like psr.tks.list**
483483
// First, recursively resolve the base member access
484484
LLVMValueRef base_value = codegen_expr_struct_access(ctx, object);
485485
if (!base_value) {
@@ -693,7 +693,20 @@ LLVMValueRef codegen_expr_struct_access(CodeGenContext *ctx, AstNode *node) {
693693
LLVMBuildStructGEP2(ctx->builder, struct_info->llvm_type, struct_ptr,
694694
field_index, "field_ptr");
695695

696-
// Load the element value
696+
// **CRITICAL FIX: Check if this field is an array**
697+
LLVMTypeRef field_type = struct_info->field_types[field_index];
698+
if (LLVMGetTypeKind(field_type) == LLVMArrayTypeKind) {
699+
// For array fields, return a pointer to the first element
700+
// This allows subsequent indexing operations to work correctly
701+
// Example: rule.move_offsets[i] where move_offsets is [int; 16]
702+
LLVMValueRef zero =
703+
LLVMConstInt(LLVMInt32TypeInContext(ctx->context), 0, false);
704+
LLVMValueRef indices[2] = {zero, zero};
705+
return LLVMBuildGEP2(ctx->builder, field_type, field_ptr, indices, 2,
706+
"array_field_ptr");
707+
}
708+
709+
// Load the element value (for non-array fields)
697710
LLVMValueRef result =
698711
LLVMBuildLoad2(ctx->builder, struct_info->field_types[field_index],
699712
field_ptr, "field_val");

std/terminal.lx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,16 @@ pub const get_terminal_size -> fn () void {
180180
system("tput lines > /tmp/luma_lines");
181181
// Read back from files...
182182
}
183+
184+
pub const get_line -> fn (prompt: *char, buffer: *char, size: int) void {
185+
output(prompt);
186+
let i: int = 0;
187+
loop (i < size - 1) : (++i) {
188+
let c: char = getch();
189+
if (c == '\n' || c == '\r') {
190+
break;
191+
}
192+
buffer[i] = c;
193+
}
194+
buffer[i] = '\0';
195+
}

0 commit comments

Comments
 (0)