diff options
| -rw-r--r-- | examples/features.sloth | 42 | ||||
| -rw-r--r-- | src/lexer.rs | 96 | ||||
| -rw-r--r-- | tour/literals.sloth | 2 |
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] |
