Skip to content

Commit

Permalink
feat: Extended stdlib with string conversions, cmd input, and random
Browse files Browse the repository at this point in the history
  • Loading branch information
alinalihassan committed May 27, 2022
1 parent 19ee297 commit 8e03a0d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cmake-build-debug-coverage/
build/

# Testing file
test.les
test*.les

# File system specific
**/.DS_Store
2 changes: 1 addition & 1 deletion src/liblesma/Backend/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ llvm::Value *Codegen::visit(Literal *node) {
else if (node->getType() == TokenType::STRING)
return Builder->CreateGlobalStringPtr(node->getValue());
else if (node->getType() == TokenType::NIL)
return ConstantPointerNull::get(PointerType::get(*TheContext, 0));
return ConstantPointerNull::getNullValue(Builder->getInt8PtrTy(0));
else if (node->getType() == TokenType::IDENTIFIER) {
// Look this variable up in the function.
auto val = Scope->lookup(node->getValue());
Expand Down
38 changes: 38 additions & 0 deletions src/std/base.les
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
def extern printf(fmt: str, ...)
def extern scanf(fmt: str, ...)
def extern malloc(size: int) -> *void
def extern strlen(x: str) -> int
def extern atoll(x: str) -> int
def extern strtod(x: str) -> float
def extern rand() -> int
def extern srand(x: int)
def extern time(x: *int) -> int

def input() -> str
return input("")

def input(prompt: str) -> str
if strlen(prompt) > 0
printf("%s", prompt)

var line: str = malloc(256) as str

# TODO: Currently only reading 255 characters, could be longer
scanf("%255[^\n]%*c",line)

return line

def strToInt(x: str) -> int
return atoll(x)

def strToFloat(x: str) -> float
return strtod(x)

def random(x: int, y: int) -> int
srand(time(null as *int))
if x < y
return x + rand() % (y - x)

return y + rand() % (x - y)

def print(x: str)
printf("%s\n", x)

def print(x: int8)
printf("%c\n", x)

def print(x: int)
printf("%d\n", x)

Expand Down

0 comments on commit 8e03a0d

Please sign in to comment.