aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdvard Thörnros <edvard.thornros@gmail.com>2021-01-09 12:34:46 +0100
committerEdvard Thörnros <edvard.thornros@gmail.com>2021-01-09 12:34:46 +0100
commite0e0ac9ea0d893442a0c5806353ebbddb8f905b4 (patch)
tree04cd58a9fa202a974d1a54f9ceed9382aec042c1
downloadsylt-e0e0ac9ea0d893442a0c5806353ebbddb8f905b4.tar.gz
Parsing numbers and inital commit
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock5
-rw-r--r--Cargo.toml9
-rw-r--r--src/main.rs11
-rw-r--r--src/tokenizer.rs87
-rw-r--r--tests/simple.tdy2
6 files changed, 115 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ea8c4bf
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..1e14c83
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,5 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "tihdy"
+version = "0.1.0"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..b51de73
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "tihdy"
+version = "0.1.0"
+authors = ["Edvard Thörnros <edvard.thornros@gmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..576ca20
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,11 @@
+mod tokenizer;
+
+fn main() {
+ println!("Hello, world!");
+
+ let tokens = tokenizer::file_to_tokens("tests/simple.tdy");
+
+ for token in tokens.iter() {
+ println!("| {:?}", token);
+ }
+}
diff --git a/src/tokenizer.rs b/src/tokenizer.rs
new file mode 100644
index 0000000..371ae94
--- /dev/null
+++ b/src/tokenizer.rs
@@ -0,0 +1,87 @@
+use std::{env, fs};
+
+#[derive(Debug)]
+pub enum TokenKind {
+ Identifier(String), String(String), Float(f64), Int(i64), Bool(bool),
+
+ If, For, In, Loop,
+
+ Plus, Minus, Star, Slash,
+ PlusPlus, MinusMinus,
+ PlusEqual, MinusEqual, StarEqual, SlashEqual,
+
+ Colon, ColonColon,
+ Equal, EqualEqual,
+
+ LeftParen, RightParen,
+
+ LeftBracket, RightBracket,
+
+ LeftBrace, RightBrace,
+
+ Greater, Less,
+ GreaterEqual, LessEqual,
+
+ Arrow,
+ Newline,
+
+ Error,
+ EOF,
+}
+
+#[derive(Debug)]
+pub struct Token <'a> {
+ kind: TokenKind,
+
+ row: i32,
+ col: i32,
+ filename: &'a str,
+}
+
+use std::iter::Peekable;
+use std::str::Chars;
+
+fn parse_number(c: char, chars: &mut Peekable<Chars>) -> TokenKind {
+ let mut number = String::from(c);
+ loop {
+ if let Some(c) = chars.peek() {
+ match *c {
+ '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8'| '9' | '.' => {}
+ _ => { break; }
+ }
+ }
+ number.push(chars.next().unwrap());
+ }
+ if number.contains(".") {
+ return TokenKind::Float(number.parse::<f64>().unwrap());
+ } else {
+ return TokenKind::Int(number.parse::<i64>().unwrap());
+ }
+}
+
+pub fn file_to_tokens(filename: &str) -> Vec<Token> {
+ let content = fs::read_to_string(filename).unwrap();
+
+ let mut tokens = Vec::new();
+
+ let mut row = 1;
+ let mut col = 0;
+
+ let mut chars = content.chars().peekable();
+ while let Some(c) = chars.next() {
+ let mut kind = match c {
+ '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8'| '9' | '.' => {
+ parse_number(c, &mut chars)
+ }
+ _ => {
+ TokenKind::Error
+ }
+ };
+
+ tokens.push(Token{kind, row, col, filename});
+ }
+
+ tokens.push(Token{kind: TokenKind::EOF, row, col, filename});
+
+ return tokens;
+}
diff --git a/tests/simple.tdy b/tests/simple.tdy
new file mode 100644
index 0000000..c0fdb77
--- /dev/null
+++ b/tests/simple.tdy
@@ -0,0 +1,2 @@
+1234
+1234.123