From daa511aa178b67041682c009026d75dd3d28326b Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Thu, 20 Apr 2023 10:40:40 +0200 Subject: [PATCH] start logos --- Cargo.lock | 290 +++++++++++++++++++++++++++++++++---------- Cargo.toml | 3 +- src/ast/function.rs | 37 ------ src/ast/mod.rs | 67 +++++----- src/ast/span.rs | 32 ----- src/ast/statement.rs | 29 ----- src/codegen.rs | 16 +-- src/lexer.rs | 0 src/main.rs | 2 + src/tokens.rs | 42 +++++++ 10 files changed, 313 insertions(+), 205 deletions(-) delete mode 100644 src/ast/function.rs delete mode 100644 src/ast/span.rs delete mode 100644 src/ast/statement.rs create mode 100644 src/lexer.rs create mode 100644 src/tokens.rs diff --git a/Cargo.lock b/Cargo.lock index 11e81303a..eb6fd1756 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,6 +36,55 @@ dependencies = [ "yansi-term", ] +[[package]] +name = "anstream" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e579a7752471abc2a8268df8b20005e3eadd975f585398f17efcfd8d4927371" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is-terminal", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" + +[[package]] +name = "anstyle-parse" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] +name = "anstyle-wincon" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bcd8291a340dd8ac70e18878bc4501dd7b4ff970cfa21c207d36ece51ea88fd" +dependencies = [ + "anstyle", + "windows-sys 0.48.0", +] + [[package]] name = "ascii-canvas" version = "3.0.0" @@ -66,6 +115,12 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "beef" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1" + [[package]] name = "bit-set" version = "0.5.3" @@ -101,39 +156,45 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.1.13" +version = "4.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c911b090850d79fc64fe9ea01e28e465f65e821e08813ced95bced72f7a8a9b" +checksum = "9b802d85aaf3a1cdb02b224ba472ebdea62014fccfcb269b95a4d76443b5ee5a" dependencies = [ - "bitflags", + "clap_builder", "clap_derive", - "clap_lex", - "is-terminal", "once_cell", +] + +[[package]] +name = "clap_builder" +version = "4.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14a1a858f532119338887a4b8e1af9c60de8249cd7bafd68036a489e261e37b6" +dependencies = [ + "anstream", + "anstyle", + "bitflags", + "clap_lex", "strsim", - "termcolor", ] [[package]] name = "clap_derive" -version = "4.1.12" +version = "4.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a932373bab67b984c790ddf2c9ca295d8e3af3b7ef92de5a5bacdccdee4b09b" +checksum = "3f9644cd56d6b87dbe899ef8b053e331c0637664e9e21a33dfcdc36093f5c5c4" dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.10", + "syn 2.0.15", ] [[package]] name = "clap_lex" -version = "0.3.3" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "033f6b7a4acb1f358c742aaca805c939ee73b4c6209ae4318ec7aca81c42e646" -dependencies = [ - "os_str_bytes", -] +checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" [[package]] name = "color-eyre" @@ -162,6 +223,12 @@ dependencies = [ "tracing-error", ] +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "crunchy" version = "0.2.2" @@ -206,6 +273,7 @@ dependencies = [ "itertools", "lalrpop", "lalrpop-util", + "logos", "regex", "tracing", "tracing-subscriber", @@ -228,13 +296,13 @@ dependencies = [ [[package]] name = "errno" -version = "0.2.8" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" dependencies = [ "errno-dragonfly", "libc", - "winapi", + "windows-sys 0.48.0", ] [[package]] @@ -263,6 +331,12 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "getrandom" version = "0.2.8" @@ -341,25 +415,25 @@ dependencies = [ [[package]] name = "io-lifetimes" -version = "1.0.9" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb" +checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220" dependencies = [ "hermit-abi", "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] name = "is-terminal" -version = "0.4.5" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8687c819457e979cc940d09cb16e42a1bf70aa6b60a549de6d3a62a0ee90c69e" +checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" dependencies = [ "hermit-abi", "io-lifetimes", "rustix", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -411,15 +485,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.140" +version = "0.2.141" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" +checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" [[package]] name = "linux-raw-sys" -version = "0.1.4" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" +checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" [[package]] name = "llvm-sys" @@ -453,6 +527,38 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "logos" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c000ca4d908ff18ac99b93a062cb8958d331c3220719c52e77cb19cc6ac5d2c1" +dependencies = [ + "logos-derive", +] + +[[package]] +name = "logos-codegen" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc487311295e0002e452025d6b580b77bb17286de87b57138f3b5db711cded68" +dependencies = [ + "beef", + "fnv", + "proc-macro2", + "quote", + "regex-syntax", + "syn 2.0.15", +] + +[[package]] +name = "logos-derive" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbfc0d229f1f42d790440136d941afd806bc9e949e2bcb8faa813b0f00d1267e" +dependencies = [ + "logos-codegen", +] + [[package]] name = "matchers" version = "0.1.0" @@ -508,12 +614,6 @@ version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" -[[package]] -name = "os_str_bytes" -version = "6.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267" - [[package]] name = "overload" version = "0.1.1" @@ -546,7 +646,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -588,9 +688,9 @@ checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" [[package]] name = "proc-macro2" -version = "1.0.53" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba466839c78239c09faf015484e5cc04860f88242cff4d03eb038f04b4699b73" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" dependencies = [ "unicode-ident", ] @@ -658,16 +758,16 @@ checksum = "d4a36c42d1873f9a77c53bde094f9664d9891bc604a45b4798fd2c389ed12e5b" [[package]] name = "rustix" -version = "0.36.11" +version = "0.37.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db4165c9963ab29e422d6c26fbc1d37f15bace6b2810221f9d925023480fcf0e" +checksum = "85597d61f83914ddeba6a47b3b8ffe7365107221c2e557ed94426489fefb5f77" dependencies = [ "bitflags", "errno", "io-lifetimes", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -741,9 +841,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.10" +version = "2.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aad1363ed6d37b84299588d62d3a7d95b5a5c2d9aad5c85609fda12afaa1f40" +checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" dependencies = [ "proc-macro2", "quote", @@ -761,15 +861,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "termcolor" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" -dependencies = [ - "winapi-util", -] - [[package]] name = "thiserror" version = "1.0.40" @@ -787,7 +878,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.10", + "syn 2.0.15", ] [[package]] @@ -899,6 +990,12 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "valuable" version = "0.1.0" @@ -927,15 +1024,6 @@ 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" @@ -948,7 +1036,16 @@ version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" dependencies = [ - "windows-targets", + "windows-targets 0.42.2", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.0", ] [[package]] @@ -957,13 +1054,28 @@ version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" 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", + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + +[[package]] +name = "windows-targets" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +dependencies = [ + "windows_aarch64_gnullvm 0.48.0", + "windows_aarch64_msvc 0.48.0", + "windows_i686_gnu 0.48.0", + "windows_i686_msvc 0.48.0", + "windows_x86_64_gnu 0.48.0", + "windows_x86_64_gnullvm 0.48.0", + "windows_x86_64_msvc 0.48.0", ] [[package]] @@ -972,42 +1084,84 @@ version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" + [[package]] name = "windows_aarch64_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" + [[package]] name = "windows_i686_gnu" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" + [[package]] name = "windows_i686_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" + [[package]] name = "windows_x86_64_gnu" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" + [[package]] name = "windows_x86_64_gnullvm" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + [[package]] name = "windows_x86_64_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" + [[package]] name = "yansi-term" version = "0.1.2" diff --git a/Cargo.toml b/Cargo.toml index 69f5926fe..eec905def 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ categories = ["compilers"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = { version = "4.1.13", features = ["derive"] } +clap = { version = "4.2.2", features = ["derive"] } color-eyre = "0.6.2" itertools = "0.10.5" lalrpop-util = { version = "0.19.9", features = ["lexer"] } @@ -21,6 +21,7 @@ tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } inkwell = { version = "0.1.1", features = ["llvm15-0"] } annotate-snippets = { version = "0.9.1", features = ["color"] } +logos = "0.13.0" [build-dependencies] lalrpop = "0.19.9" diff --git a/src/ast/function.rs b/src/ast/function.rs deleted file mode 100644 index a59e28f26..000000000 --- a/src/ast/function.rs +++ /dev/null @@ -1,37 +0,0 @@ -use super::{Identifier, SpanValue, Statement}; - -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Parameter { - pub ident: Identifier, - pub type_name: Identifier, -} - -impl Parameter { - pub fn new(ident: Identifier, type_name: Identifier) -> Self { - Self { ident, type_name } - } -} - -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Function { - pub ident: Identifier, - pub params: Vec, - pub body: Vec>, - pub return_type: Option, -} - -impl Function { - pub fn new( - ident: Identifier, - params: Vec, - body: Vec>, - return_type: Option, - ) -> Self { - Self { - ident, - params, - body, - return_type, - } - } -} diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 0f27e3e09..2ac53c767 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1,11 +1,3 @@ -pub mod function; -pub mod span; -pub mod statement; - -pub use function::*; -pub use span::*; -pub use statement::*; - #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum OpCode { Add, @@ -28,41 +20,56 @@ impl OpCode { } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Identifier(pub SpanValue); - -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Number(pub SpanValue); - -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum Term { - Identifier(Identifier), - Number(Number), +pub enum LiteralValue { + String, + Integer { + bits: usize, + signed: bool + }, } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum Expr { - Term(Term), - Call(Identifier, Vec>>), - Op(SpanValue>, OpCode, SpanValue>), +pub enum Expression { + Literal(LiteralValue), + Variable(String), + Call { + function: String, + args: Vec> + }, + BinaryOp(Box, OpCode, Box), } -impl Expr { - pub fn new_ident(value: Identifier) -> Self { - Self::Term(Term::Identifier(value)) - } +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Parameter { + pub ident: String, + pub type_name: String, +} - pub fn new_number(value: Number) -> Self { - Self::Term(Term::Number(value)) - } +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Function { + pub name: String, + pub params: Vec, + pub body: Vec, + pub return_type: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Statement { + Variable { + name: String, + value: Box + }, + Return(Option>), + Function(Function), } #[derive(Debug, Clone)] pub struct Program { - pub statements: Vec>, + pub statements: Vec, } impl Program { - pub fn new(statements: Vec>) -> Self { + pub fn new(statements: Vec) -> Self { Self { statements } } } diff --git a/src/ast/span.rs b/src/ast/span.rs deleted file mode 100644 index c74efdf06..000000000 --- a/src/ast/span.rs +++ /dev/null @@ -1,32 +0,0 @@ -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Span { - pub from: usize, - pub to: usize, -} - -impl Span { - pub fn new(from: usize, to: usize) -> Self { - Self { from, to } - } -} - -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct SpanValue { - pub span: Span, - pub value: T, -} - -impl SpanValue { - pub fn new(l: usize, value: T, r: usize) -> Self { - Self { - span: Span::new(l, r), - value, - } - } -} - -impl From for (usize, usize) { - fn from(value: Span) -> Self { - (value.from, value.to) - } -} diff --git a/src/ast/statement.rs b/src/ast/statement.rs deleted file mode 100644 index 3a848be42..000000000 --- a/src/ast/statement.rs +++ /dev/null @@ -1,29 +0,0 @@ -use super::{Expr, Function, Identifier, SpanValue}; - -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct StatementBody { - pub ident: Identifier, - pub expr: SpanValue>, -} - -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum Statement { - Assignment(StatementBody), - Definition(StatementBody), - Return(Option>>), - Function(Function), -} - -impl Statement { - pub fn new_assignment(ident: Identifier, expr: SpanValue>) -> Self { - Self::Assignment(StatementBody { ident, expr }) - } - - pub fn new_definition(ident: Identifier, expr: SpanValue>) -> Self { - Self::Definition(StatementBody { ident, expr }) - } - - pub fn new_function(func: Function) -> Self { - Self::Function(func) - } -} diff --git a/src/codegen.rs b/src/codegen.rs index 07dea579f..bf2612563 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -13,7 +13,7 @@ use itertools::{Either, Itertools}; use statement::Statement; -use crate::ast::{self, statement, Expr, Function, Identifier, OpCode, SpanValue, Term}; +use crate::ast::{self, statement, Expression, Function, Identifier, OpCode, SpanValue, Term}; #[derive(Debug, Clone)] pub struct ProgramData { @@ -199,13 +199,13 @@ impl<'ctx> CodeGen<'ctx> { pub fn compile_expression( &self, block: &BasicBlock, - expr: &SpanValue>, + expr: &SpanValue>, variables: &mut HashMap>, ) -> Result>> { Ok(match &*expr.value { - Expr::Term(term) => Some(self.compile_term(term, variables)?), - Expr::Call(func_id, args) => self.compile_call(block, func_id, args, variables)?, - Expr::Op(lhs, op, rhs) => Some(self.compile_op(block, lhs, op, rhs, variables)?), + Expression::Term(term) => Some(self.compile_term(term, variables)?), + Expression::Call(func_id, args) => self.compile_call(block, func_id, args, variables)?, + Expression::Op(lhs, op, rhs) => Some(self.compile_op(block, lhs, op, rhs, variables)?), }) } @@ -213,7 +213,7 @@ impl<'ctx> CodeGen<'ctx> { &self, block: &BasicBlock, func_id: &Identifier, - args: &[SpanValue>], + args: &[SpanValue>], variables: &mut HashMap>, ) -> Result>> { let func_name = &func_id.0.value; @@ -242,9 +242,9 @@ impl<'ctx> CodeGen<'ctx> { pub fn compile_op( &self, block: &BasicBlock, - lhs: &SpanValue>, + lhs: &SpanValue>, op: &OpCode, - rhs: &SpanValue>, + rhs: &SpanValue>, variables: &mut HashMap>, ) -> Result> { let lhs = self diff --git a/src/lexer.rs b/src/lexer.rs new file mode 100644 index 000000000..e69de29bb diff --git a/src/main.rs b/src/main.rs index a889008d1..a90e8f76e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,8 @@ use crate::codegen::ProgramData; pub mod ast; pub mod check; pub mod codegen; +pub mod tokens; +pub mod lexer; lalrpop_mod!(pub grammar); diff --git a/src/tokens.rs b/src/tokens.rs new file mode 100644 index 000000000..d8792aad8 --- /dev/null +++ b/src/tokens.rs @@ -0,0 +1,42 @@ +use std::fmt; +use logos::Logos; + +#[derive(Logos, Debug, PartialEq)] +#[logos(skip r"[ \t\n\f]+", skip r"#.*\n?")] +pub enum Token { + #[token("var")] + KeywordVar, + #[token("print")] + KeywordPrint, + + #[regex("[_a-zA-Z][_0-9a-zA-Z]*", |lex| lex.slice().parse().ok())] + Identifier(String), + #[regex(r"\d+", |lex| lex.slice().parse().ok())] + Integer(i64), + + #[token("(")] + LParen, + #[token(")")] + RParen, + #[token("=")] + Assign, + #[token(";")] + Semicolon, + + #[token("+")] + OperatorAdd, + #[token("-")] + OperatorSub, + #[token("*")] + OperatorMul, + #[token("/")] + OperatorDiv, + #[token("%")] + OperatorRem, +} + +impl fmt::Display for Token { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } + }