From aa8e101800fd0db83b70bbf6a897bc67a5330fc7 Mon Sep 17 00:00:00 2001 From: Charlie Root Date: Wed, 26 Mar 2025 21:41:27 +0100 Subject: [PATCH] initial commit --- .envrc | 2 ++ .gitignore | 3 ++ Cargo.lock | 7 +++++ Cargo.toml | 6 ++++ flake.lock | 43 +++++++++++++++++++++++++ flake.nix | 35 +++++++++++++++++++++ nix/package.nix | 18 +++++++++++ src/main.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 197 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 nix/package.nix create mode 100644 src/main.rs diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..e3fecb3 --- /dev/null +++ b/.envrc @@ -0,0 +1,2 @@ +use flake + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ddbf5b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target + +/.direnv diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..f9fd79c --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "ralc" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3b758b0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ralc" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..957dc1d --- /dev/null +++ b/flake.lock @@ -0,0 +1,43 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1742889210, + "narHash": "sha256-hw63HnwnqU3ZQfsMclLhMvOezpM7RSB0dMAtD5/sOiw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "698214a32beb4f4c8e3942372c694f40848b360d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "systems": "systems" + } + }, + "systems": { + "locked": { + "lastModified": 1689347949, + "narHash": "sha256-12tWmuL2zgBgZkdoB6qXZsgJEH9LR3oUgpaQq2RbI80=", + "owner": "nix-systems", + "repo": "default-linux", + "rev": "31732fcf5e8fea42e59c2488ad31a0e651500f68", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default-linux", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..27fc7c2 --- /dev/null +++ b/flake.nix @@ -0,0 +1,35 @@ +{ + description = "ralc, a rust calculator"; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + systems.url = "github:nix-systems/default-linux"; + }; + + outputs = inputs: let + pkgs = inputs.nixpkgs.legacyPackages.x86_64-linux; + eachSystem = inputs.nixpkgs.lib.genAttrs (import inputs.systems); + pkgsFor = inputs.nixpkgs.legacyPackages; + in { + packages = eachSystem (system: { + default = inputs.self.packages.${system}.ralc; + ralc = pkgsFor.${system}.callPackage ./nix/package.nix {}; + }); + + devShells = eachSystem (system: { + default = pkgsFor.${system}.mkShell { + packages = builtins.attrValues { + inherit + (pkgsFor.${system}) + cargo + rustc + rustfmt + ; + inherit + (pkgsFor.${system}.rustPackages) + clippy + ; + }; + }; + }); + }; +} diff --git a/nix/package.nix b/nix/package.nix new file mode 100644 index 0000000..71a9089 --- /dev/null +++ b/nix/package.nix @@ -0,0 +1,18 @@ +{ + rustPlatform, + lib, + ... +}: let + toml = (lib.importTOML ../Cargo.toml).package; + pname = toml.name; + inherit (toml) version; +in + rustPlatform.buildRustPackage { + inherit pname version; + src = builtins.path { + name = "${pname}-${version}"; + path = lib.sources.cleanSource ../.; + }; + cargoLock.lockFile = ../Cargo.lock; + doCheck = false; + } diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..4bfbcd3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,83 @@ +use std::io; + +#[derive(Debug)] +enum Symbol { + Number, + + LeftBracket, + RightBracket, + Add, + Sub, + Mul, + Div, +} + +#[derive(Debug)] +enum ParseError { + WrongTokenError { pos: usize }, +} + +fn main() { + loop { + let user_input = get_user_input().expect("No input"); + match tokenize(&user_input) { + Ok(tokenized_input) => { + for el in &tokenized_input { + println!("{:?}", el); + } + } + Err(ParseError::WrongTokenError { pos }) => { + for _ in 0..pos { + print!("-"); + } + println!("^"); + println!("Syntax Error\n"); + } + } + } +} + +fn get_user_input() -> io::Result { + let mut buffer = String::new(); + let stdin = io::stdin(); + stdin.read_line(&mut buffer)?; + Ok(buffer) +} + +fn tokenize(input: &str) -> Result, ParseError> { + let mut tokens: Vec = vec![]; + + for (i, c) in input.chars().enumerate() { + match c { + ' ' => { + continue; + } + '(' => { + tokens.push(Symbol::LeftBracket); + } + ')' => { + tokens.push(Symbol::RightBracket); + } + '+' => { + tokens.push(Symbol::Add); + } + '-' => { + tokens.push(Symbol::Sub); + } + '*' => { + tokens.push(Symbol::Mul); + } + '/' => { + tokens.push(Symbol::Div); + } + '0'..='9' => { + tokens.push(Symbol::Number); + } + '\n' => { + break; + } + _ => return Err(ParseError::WrongTokenError { pos: i }), + } + } + Ok(tokens) +}