aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCody <cody@codyq.dev>2023-03-27 04:47:00 -0500
committerCody <cody@codyq.dev>2023-03-27 04:47:00 -0500
commitbb95375f8b24141bf7dfe5a8b1bba5c995f61253 (patch)
tree10eb423cf881afaba7d854f8150a8f5d55d6d1db
parentff2d00dec2317df8de0afaf56beb35e2edb70cd7 (diff)
downloadsloth-bb95375f8b24141bf7dfe5a8b1bba5c995f61253.tar.gz
hm
-rw-r--r--examples/features.sloth42
-rw-r--r--src/lexer.rs96
-rw-r--r--tour/literals.sloth2
3 files changed, 127 insertions, 13 deletions
diff --git a/examples/features.sloth b/examples/features.sloth
new file mode 100644
index 0000000..26be73c
--- /dev/null
+++ b/examples/features.sloth
@@ -0,0 +1,42 @@
+fn calories(input) {
+ var elves = []
+ var current = 0
+
+ for line in input.lines() {
+ if line.empty() {
+ elves.append(current)
+ current = 0
+ continue
+ }
+
+ current += line as!! int
+ }
+
+ elves.sort()
+ elves.reverse()
+
+ return elves[0..3].sum()
+}
+
+fn fib(x: int) {
+ if x < 2 {
+ return x
+ }
+
+ return fib(x - 1) + fib (x - 2)
+}
+
+fn codes(input: String): List<String> {
+ val chars = input.chars()
+ .windowed(4)
+ .map(it -> it as Set)
+ .filter(-> $0.len() == 4)
+ .map(it -> it.join())
+ return chars
+}
+
+## Will convert celsius to fahrenheit
+fn fahrenheit(celsius) {
+ return 32.0 + celsius * 1.8
+}
+
diff --git a/src/lexer.rs b/src/lexer.rs
index ef79716..8631eef 100644
--- a/src/lexer.rs
+++ b/src/lexer.rs
@@ -14,18 +14,82 @@ pub enum TokenType {
DocComment,
Comment,
- // Operatiors
- Plus,
- Minus,
- Star,
- Slash,
- Perc,
-
- PlusEq,
- MinusEq,
- StarEq,
- SlashEq,
- PercEq,
+ // Brackets
+ OpeningParen, // (
+ ClosingParen, // )
+ OpeningBracket, // [
+ ClosingBracket, // ]
+ OpeningBrace, // {
+ ClosingBrace, // }
+
+ // Operators
+ Plus, // +
+ PlusPlus, // ++
+ Minus, // -
+ Star, // *
+ StarStar, // **
+ Slash, // /
+ Perc, // %
+ Tilde, // ~
+
+ PlusEq, // +=
+ PlusPlusEq, // ++=
+ MinusEq, // -=
+ StarEq, // *=
+ StarStarEq, // **=
+ SlashEq, // /=
+ PercEq, // %=
+
+ Amp, // &
+ AmpAmp, // &&
+ Pipe, // |
+ PipePipe, // ||
+
+ Eq, // =
+ EqEq, // ==
+ Bang, // !
+ BangBang, // !!
+ BangEq, // !=
+
+ Lt, // <
+ LtLt, // <<
+ LtEq, // <=
+ Gt, // >
+ GtGt, // >>
+ GtEq, // >=
+
+ Comma,
+
+ Question, // ?
+ QuestionDot, // ?.
+ QuestionQuestion, // ??
+ Dot, // .
+ DotDot, // ..
+
+ Colon, // :
+ ColonColon, // ::
+ SemiColon, // ;
+
+ Arrow, // ->
+
+ // Keywords
+ Val,
+ Var,
+
+ Fn,
+
+ If,
+ Else,
+
+ While,
+ For,
+ In,
+
+ Loop,
+ Break,
+ Continue,
+
+ As,
// Misc
Literal(Literal),
@@ -79,3 +143,11 @@ impl<'a> Iterator for Lexer<'a> {
unimplemented!()
}
}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn basic_test_a() {
+ //
+ }
+}
diff --git a/tour/literals.sloth b/tour/literals.sloth
index 9109645..53e29cd 100644
--- a/tour/literals.sloth
+++ b/tour/literals.sloth
@@ -68,7 +68,7 @@ if /[0-9]/ in "24" {} # impl Contains<Regex> for String
maps["foo"] # Option<i32>
maps["foo"]!! # 48 - Panics in None case
maps["foo"]! # 48 - Caller of function is responsible for None case
-maps["foo"]?.signum() ?: 0 # 1 - Provide a default for None case
+maps["foo"]?.signum() ?? 0 # 1 - Provide a default for None case
maps.keys() # ["foo", "bar"]
maps.values() # [48, 97]