Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Aug 19, 2023
1 parent 7275a60 commit 1e1aeb4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Build Sloth
cargo build
#cargo build
FILENAME="$1"
# Compile standard library
./target/debug/sloth std/extern.sloth std/stdmath.sloth std/stdio.sloth $FILENAME

# Generate binary
clang --verbose -lm output.o std/stdio.c std/stdlib.c std/stdmath.c -o "${FILENAME%.sloth}"
rm output.o

# Move file
mv "${FILENAME%.sloth}" .
5 changes: 5 additions & 0 deletions sloth/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ pub enum StmtKind {
identifier: String,
value: Expr,
},
DefineValue {
identifier: String,
value: Expr,
typ: Option<TypeIdentifier>,
},
/// A function definition. Output is None when the function returns nothing
/// meaning void, otherwise it is the name of the type the function
/// returns.
Expand Down
32 changes: 32 additions & 0 deletions sloth/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl<'a> AstParser<'a> {
TokenType::While => self.while_stmt(),
TokenType::For => self.for_stmt(),
TokenType::Var => self.define_variable(),
TokenType::Val => self.define_value(),
TokenType::Fn => self.define_function(false),
TokenType::Return => self.return_stmt(),

Expand Down Expand Up @@ -116,6 +117,37 @@ impl<'a> AstParser<'a> {
))
}

fn define_value(&mut self) -> Result<Stmt, ParsingError> {
// Consume the var token
self.consume(TokenType::Val, "Expected val")?;

// Get the identifier and type
let identifier = self.consume_identifier()?;
let typ = if self.consume(TokenType::Colon, "Expected ':'").is_ok() {
self.consume_type().ok()
} else {
None
};

// Get the default value
self.consume(TokenType::Eq, "Expected '='")?;
let value = self.expression()?;

self.consume(TokenType::SemiColon, "Expected ';' at end of statement")?;

let kind = StmtKind::DefineVariable {
identifier,
value,
typ,
};

Ok(Stmt::new(
self.reserve_id(),
self.line,
kind,
self.top.clone(),
))
}
// TODO: Make variable types optional
fn define_variable(&mut self) -> Result<Stmt, ParsingError> {
// Consume the var token
Expand Down

0 comments on commit 1e1aeb4

Please sign in to comment.