initial commit

This commit is contained in:
Charlie Root 2025-03-26 21:41:27 +01:00
commit aa8e101800
Signed by: faukah
SSH key fingerprint: SHA256:Uj2AXqvtdCA4hn5Hq0ZonhIAyUqI1q4w2sMG3Z1TH7E
8 changed files with 197 additions and 0 deletions

2
.envrc Normal file
View file

@ -0,0 +1,2 @@
use flake

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
/.direnv

7
Cargo.lock generated Normal file
View file

@ -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"

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "ralc"
version = "0.1.0"
edition = "2024"
[dependencies]

43
flake.lock generated Normal file
View file

@ -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
}

35
flake.nix Normal file
View file

@ -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
;
};
};
});
};
}

18
nix/package.nix Normal file
View file

@ -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;
}

83
src/main.rs Normal file
View file

@ -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<String> {
let mut buffer = String::new();
let stdin = io::stdin();
stdin.read_line(&mut buffer)?;
Ok(buffer)
}
fn tokenize(input: &str) -> Result<Vec<Symbol>, ParseError> {
let mut tokens: Vec<Symbol> = 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)
}