Init commit
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "calc"
|
||||||
|
version = "0.1.0"
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "calc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{ pkgs ? import <nixpkgs> {} }: with pkgs; mkShell {
|
||||||
|
buildInputs = [
|
||||||
|
cargo
|
||||||
|
rustc
|
||||||
|
rust-analyzer
|
||||||
|
pkg-config
|
||||||
|
];
|
||||||
|
}
|
||||||
+75
@@ -0,0 +1,75 @@
|
|||||||
|
use core::fmt;
|
||||||
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let stdin = io::stdin();
|
||||||
|
let mut buffer = String::new();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
print!("> ");
|
||||||
|
io::stdout().flush().unwrap();
|
||||||
|
|
||||||
|
stdin.read_line(&mut buffer).unwrap();
|
||||||
|
|
||||||
|
let tokens = tokenisator(&buffer);
|
||||||
|
|
||||||
|
buffer.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tokenisator(task: &str) -> Vec<Token> {
|
||||||
|
let mut tokens = Vec::<Token>::new();
|
||||||
|
let chars: Vec<char> = task.chars().collect();
|
||||||
|
let mut pointer = 0;
|
||||||
|
|
||||||
|
while pointer < task.len() {
|
||||||
|
dbg!(pointer, task.len());
|
||||||
|
|
||||||
|
let c = chars[pointer];
|
||||||
|
match c {
|
||||||
|
'0' .. '9' => {
|
||||||
|
let mut end = pointer;
|
||||||
|
while end < task.len() {
|
||||||
|
let cur_c = chars[end];
|
||||||
|
if !cur_c.is_ascii_digit() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
end += 1;
|
||||||
|
}
|
||||||
|
tokens.push(Token::Number(
|
||||||
|
task.get(pointer..end).unwrap().parse::<i32>().unwrap()
|
||||||
|
));
|
||||||
|
pointer = end-1;
|
||||||
|
}
|
||||||
|
'+' => tokens.push(Token::Addition),
|
||||||
|
'-' => tokens.push(Token::Substruction),
|
||||||
|
'*' => tokens.push(Token::Multiplication),
|
||||||
|
'/' => tokens.push(Token::Division),
|
||||||
|
'(' => tokens.push(Token::BracketOpen),
|
||||||
|
')' => tokens.push(Token::BracketClose),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
pointer += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", task);
|
||||||
|
|
||||||
|
for t in &tokens {
|
||||||
|
println!("{:?}", t);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Token {
|
||||||
|
Number(i32),
|
||||||
|
Addition,
|
||||||
|
Substruction,
|
||||||
|
Multiplication,
|
||||||
|
Division,
|
||||||
|
BracketOpen,
|
||||||
|
BracketClose,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user