-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
343 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Compile-time evaluating if statements. | ||
|
||
import "./ast.jou" | ||
import "./errors_and_warnings.jou" | ||
import "stdlib/str.jou" | ||
import "stdlib/mem.jou" | ||
|
||
|
||
# Return values: 1=true, 0=false, -1=unknown | ||
def get_special_constant(name: byte*) -> int: | ||
if strcmp(name, "WINDOWS") == 0: | ||
return WINDOWS as int | ||
if strcmp(name, "MACOS") == 0: | ||
return MACOS as int | ||
return -1 | ||
|
||
|
||
def evaluate_condition(expr: AstExpression*) -> bool: | ||
if expr->kind == AstExpressionKind::GetVariable: | ||
v = get_special_constant(expr->varname) | ||
if v == 1: | ||
return True | ||
if v == 0: | ||
return False | ||
fail(expr->location, "cannot evaluate condition at compile time") | ||
|
||
|
||
# returns the statements to replace if statement with | ||
def evaluate_compile_time_if_statement(if_stmt: AstIfStatement*) -> AstBody: | ||
result = &if_stmt->else_body | ||
for p = if_stmt->if_and_elifs; p < &if_stmt->if_and_elifs[if_stmt->n_if_and_elifs]; p++: | ||
if evaluate_condition(&p->condition): | ||
result = &p->body | ||
break | ||
|
||
ret = *result | ||
*result = AstBody{} # avoid double-free | ||
return ret | ||
|
||
|
||
# Replace body->statements[i] with zero or more statements from another body. | ||
def replace(body: AstBody*, i: int, new: AstBody) -> void: | ||
body->statements[i].free() | ||
|
||
item_size = sizeof(body->statements[0]) | ||
body->statements = realloc(body->statements, (body->nstatements + new.nstatements) * item_size) | ||
memmove(&body->statements[i + new.nstatements], &body->statements[i+1], (body->nstatements - (i+1)) * item_size) | ||
memcpy(&body->statements[i], new.statements, new.nstatements * item_size) | ||
|
||
free(new.statements) | ||
body->nstatements-- | ||
body->nstatements += new.nstatements | ||
|
||
|
||
# This handles nested if statements. | ||
def evaluate_compile_time_if_statements_in_body(body: AstBody*) -> void: | ||
i = 0 | ||
while i < body->nstatements: | ||
if body->statements[i].kind == AstStatementKind::If: | ||
replace(body, i, evaluate_compile_time_if_statement(&body->statements[i].if_statement)) | ||
else: | ||
i++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.