Ch 1.5 (finished chapter 1)

This commit is contained in:
Devon Tingley 2023-03-02 08:50:42 -05:00
parent 9ffab45bd6
commit 0fd41b6890
6 changed files with 439 additions and 11 deletions

285
Cargo.lock generated
View file

@ -8,12 +8,82 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cc"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d7ae14b20b94cb02149ed21a86c423859cbe18dc7ed69845cace50e52b40a5"
dependencies = [
"bitflags",
"clap_derive",
"clap_lex",
"is-terminal",
"once_cell",
"strsim",
"termcolor",
]
[[package]]
name = "clap_derive"
version = "4.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44bec8e5c9d09e439c4335b1af0abaab56dcf3b94999a936e1bb47b9134288f0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "350b9cf31731f9957399229e9b2adc51eeabdfbe9d71d9a0552275fd12710d09"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "errno"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
dependencies = [
"errno-dragonfly",
"libc",
"winapi",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]] [[package]]
name = "futures" name = "futures"
version = "0.3.26" version = "0.3.26"
@ -109,6 +179,52 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]]
name = "io-lifetimes"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1abeb7a0dd0f8181267ff8adc397075586500b81b28a73e8a0208b00fc170fb3"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "is-terminal"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21b6b32576413a8e69b90e952e4a026476040d81017b80445deda5f2d3921857"
dependencies = [
"hermit-abi",
"io-lifetimes",
"rustix",
"windows-sys",
]
[[package]]
name = "libc"
version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "linux-raw-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.5.0"
@ -119,9 +235,22 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
name = "moose" name = "moose"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap",
"rstest", "rstest",
] ]
[[package]]
name = "once_cell"
version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "os_str_bytes"
version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.9" version = "0.2.9"
@ -134,6 +263,30 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.51" version = "1.0.51"
@ -187,6 +340,20 @@ dependencies = [
"semver", "semver",
] ]
[[package]]
name = "rustix"
version = "0.36.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f43abb88211988493c1abb44a70efa56ff0ce98f233b7b276146f1f3f7ba9644"
dependencies = [
"bitflags",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]] [[package]]
name = "semver" name = "semver"
version = "1.0.16" version = "1.0.16"
@ -202,6 +369,12 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.109" version = "1.0.109"
@ -213,8 +386,120 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "termcolor"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.6" version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
[[package]]
name = "windows_i686_gnu"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
[[package]]
name = "windows_i686_msvc"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"

View file

@ -1,9 +1,12 @@
[package] [package]
name = "moose" name = "moose"
version = "0.1.0" version = "0.1.0"
authors =["Devon Tingley <dtingley@twilit.io>"]
description = "Oxidized interpretor for the monkey programming language"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.1.8", features = ["derive"] }
[dev-dependencies] [dev-dependencies]
rstest = "0.16.0" rstest = "0.16.0"

7
examples/basic.ms Normal file
View file

@ -0,0 +1,7 @@
print(5 + 5);
if (5 == 5) {
print(true);
} else {
print(false);
}

View file

@ -17,8 +17,30 @@ pub fn tokenize(input: &str) -> Vec<Token> {
fn next_token(input: &mut Peekable<Chars>) -> Option<Token> { fn next_token(input: &mut Peekable<Chars>) -> Option<Token> {
let tok = match input.next()? { let tok = match input.next()? {
'=' => Token::Assign,
'+' => Token::Plus, '+' => Token::Plus,
'-' => Token::Minus,
'*' => Token::Asterisk,
'/' => Token::ForwardSlash,
'!' => {
if input.peek() == Some(&'=') {
input.next();
Token::NotEqual
} else {
Token::Bang
}
}
'=' => {
if input.peek() == Some(&'=') {
input.next();
Token::Equal
} else {
Token::Assign
}
}
'<' => Token::LessThan,
'>' => Token::GreaterThan,
',' => Token::Comma, ',' => Token::Comma,
';' => Token::Semicolon, ';' => Token::Semicolon,
@ -56,8 +78,13 @@ fn read_ident(input: &mut Peekable<Chars>, first: char) -> Token {
// Check if our ident is a keyword // Check if our ident is a keyword
let ident = toks.iter().cloned().collect::<String>(); let ident = toks.iter().cloned().collect::<String>();
match ident.as_str() { match ident.as_str() {
"true" => Token::True,
"false" => Token::False,
"fn" => Token::Function, "fn" => Token::Function,
"let" => Token::Let, "let" => Token::Let,
"if" => Token::If,
"else" => Token::Else,
"return" => Token::Return,
ident => Token::Ident(ident.to_owned()), ident => Token::Ident(ident.to_owned()),
} }
@ -102,8 +129,60 @@ mod tests {
Token::Semicolon, Token::Semicolon,
])] ])]
#[case( #[case(
" "!-/*5; 5 < 10 > 5;",
let five = 5; vec![
Token::Bang,
Token::Minus,
Token::ForwardSlash,
Token::Asterisk,
Token::Int(5),
Token::Semicolon,
Token::Int(5),
Token::LessThan,
Token::Int(10),
Token::GreaterThan,
Token::Int(5),
Token::Semicolon,
])]
#[case(
"5 != 6",
vec![
Token::Int(5),
Token::NotEqual,
Token::Int(6),
])]
#[case(
"8 == 8",
vec![
Token::Int(8),
Token::Equal,
Token::Int(8),
])]
#[case(
"if (5 < 7) {
return true
} else {
return false
}",
vec![
Token::If,
Token::LeftParenthesis,
Token::Int(5),
Token::LessThan,
Token::Int(7),
Token::RightParenthesis,
Token::LeftBrace,
Token::Return,
Token::True,
Token::RightBrace,
Token::Else,
Token::LeftBrace,
Token::Return,
Token::False,
Token::RightBrace
])]
#[case(
"let five = 5;
let ten = 10; let ten = 10;
let add = fn(x, y) { let add = fn(x, y) {

View file

@ -7,8 +7,18 @@ pub enum Token {
Int(i64), Int(i64),
// Operators // Operators
Bang,
Assign, Assign,
Plus, Plus,
Minus,
Asterisk,
ForwardSlash,
Equal,
NotEqual,
LessThan,
GreaterThan,
// Delimiters // Delimiters
Comma, Comma,
@ -20,6 +30,12 @@ pub enum Token {
RightBrace, RightBrace,
// Keywords // Keywords
True,
False,
Function, Function,
Let, Let,
If,
Else,
Return,
} }

View file

@ -1,5 +1,43 @@
use std::fs;
use std::io::{self, Write};
use clap::{CommandFactory, Parser};
mod lexer; mod lexer;
fn main() { #[derive(Parser)]
println!("{:?}", lexer::tokenize("asdf")); #[command(author, version, about)]
struct Args {
path: Option<String>,
}
fn main() {
let args = Args::parse();
let cmd = Args::command();
match args.path {
Some(path) => {
let input = fs::read_to_string(&path).unwrap();
let tokens = lexer::tokenize(&input);
println!("{:?}", tokens);
}
None => start_repl(cmd.get_version().unwrap()),
}
}
fn start_repl(version: &str) {
println!("Moose {} REPL", version);
let mut input = String::new();
loop {
print!("> ");
io::stdout().flush().expect("failed to write to stdout");
io::stdin()
.read_line(&mut input)
.expect("failed to read from stdin");
let tokens = lexer::tokenize(&input);
println!("{:?}", tokens);
}
} }