From 4a683af2580225ba2d27c9ed35a0d7a76a8be038 Mon Sep 17 00:00:00 2001 From: kaiserthe13th Date: Sat, 5 Feb 2022 15:19:50 +0300 Subject: [PATCH 1/3] done! --- src/lexer/lexer.rs | 46 +++++++-- src/parser/parser.rs | 12 +++ src/runtime/runtime.rs | 219 +++++++++++++++++++++++++++++++++++++++++ src/store.rs | 1 - src/token/tokens.rs | 6 ++ 5 files changed, 276 insertions(+), 8 deletions(-) diff --git a/src/lexer/lexer.rs b/src/lexer/lexer.rs index f9654ba..2bf199d 100644 --- a/src/lexer/lexer.rs +++ b/src/lexer/lexer.rs @@ -50,17 +50,17 @@ impl Lexer { Ok(f) => { tmp_visited.push(path.display().to_string()); f - }, + } Err(FSErr::IsADir) => { path.push("giriş.trl"); match read_file(&path) { Ok(f) => { tmp_visited.push(path.display().to_string()); f - }, - Err(e) => panic!("{:?}", e) + } + Err(e) => panic!("{:?}", e), } - }, + } }; if !in_vec(&path.display().to_string(), &visited.clone()) { let mut nl = Lexer::new(source); @@ -108,10 +108,42 @@ impl Lexer { } else { *visited = tmp_visited; } - }, - _ => panic!("yükle den sonra bekleniyordu ancak bulunamadı"), + } + TokenType::ParenL => { + tokens.push(Token::new( + TokenType::InScopeParentL, + next_token.lexeme, + next_token.line, + next_token.col, + next_token.file, + Precedence::Reserved, + )); + current += 2; + while current < prog.len() - 1 { + let c = prog.get(current).unwrap().clone(); + current += 1; + match c.typ { + TokenType::ParenR => { + tokens.push(Token::new( + TokenType::InScopeParentR, + c.lexeme, + c.line, + c.col, + c.file, + Precedence::Reserved, + )); + break; + } + TokenType::Identifier | TokenType::Çarpı | TokenType::İkiNokta => tokens.push(c), + _ => panic!("yükle ('dan sonra tanımlayıcı, `)` veya `*` bekleniyordu ancak bulunamadı. {:?}", c), + } + } + } + _ => { + panic!("yükle den sonra veya `(` bekleniyordu ancak bulunamadı") + } } - }, + } _ => { tokens.push(c); current += 1; diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 7fc9dd1..9bbbb4f 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -113,6 +113,18 @@ impl Parser { for (ip, ptoken) in self.tokens.iter().enumerate() { match ptoken.typ { + LexTokenType::InScopeParentR => parsed.push(Token::new( + TokenType::InScopeParentR, + ptoken.line, + ptoken.col, + ptoken.file.clone(), + )), + LexTokenType::InScopeParentL => parsed.push(Token::new( + TokenType::InScopeParentL, + ptoken.line, + ptoken.col, + ptoken.file.clone(), + )), LexTokenType::İkiNokta => parsed.push(Token::new( TokenType::İkiNokta, ptoken.line, diff --git a/src/runtime/runtime.rs b/src/runtime/runtime.rs index e48c4ac..281d68f 100644 --- a/src/runtime/runtime.rs +++ b/src/runtime/runtime.rs @@ -38,6 +38,225 @@ impl Run { let token = self.program.get_mut(self.current).unwrap(); match token.typ.clone() { + TokenType::InScopeParentL => { + stack.new_stack(); + + let tok = self.program.get(self.current + 1).unwrap(); + match tok.typ { + TokenType::Identifier { ref id } => { + match hashs.get(id) { + Some(o) => stack.push(o.clone()), + None => return Err((stack, hashs.clone(), match get_lang() { + SupportedLanguage::Turkish => ErrorGenerator::error( + "BilinmeyenTanımlayıcı", + &format!( + "bilinmeyen değişken: `{}`, bu değişken bulunamamıştır", + tokenc.repr() + ), + tokenc.line, + tokenc.col, + tokenc.file, + { + let mut hashk = hashs.clone().into_keys(); + hashk.sort(); + let n = hashk.binary_search(id).unwrap_err(); + if hashk.is_empty() { + None + } else { + Some(format!("`{}` demek mi istediniz?", hashk[n])) + } + } + ), + SupportedLanguage::English => ErrorGenerator::error( + "UnknownIdentifier", + &format!("unknown identifier: `{}`, this identifier could not be found", tokenc.repr()), + tokenc.line, + tokenc.col, + tokenc.file, + { + let mut hashk = hashs.clone().into_keys(); + hashk.sort(); + let n = hashk.binary_search(id).unwrap_err(); + if hashk.is_empty() { + None + } else { + Some(format!("maybe you meant {}?", hashk[n])) + } + }, + ), + })), + } + } + _ => return Err((stack, hashs, match get_lang() { + SupportedLanguage::Turkish => ErrorGenerator::error( + "BeklenmedikSimge", + &format!( + "tanımlayıcı beklenmişti ancak `{}` bulundu", + tok.repr() + ), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ), + SupportedLanguage::English => ErrorGenerator::error( + "UnexpectedToken", + &format!( + "expected identifier, but found `{}`", + tok.repr() + ), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ), + }, + )), + } + + self.current += 2; + + let mut latest_id = None; + while self.program.len() > self.current { + let c = self.program.get(self.current).unwrap().clone(); + + match c.typ { + TokenType::İkiNokta => { + let a = match stack.pop() { + Some(a) => a, + None => return Err((stack, hashs, match get_lang() { + SupportedLanguage::Turkish => { + ErrorGenerator::error( + "KümedeYeterliDeğişkenYok", + &format!("kümede yeterli değişken bulunmadığından dolayı `{}` operatörü uygulanamamıştır", tokenc.repr()), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ) + } + SupportedLanguage::English => { + ErrorGenerator::error( + "NotEnoughVarsInStack", + &format!("because there weren't enough variables in the stack, the operator `{}` couldn't be used", tokenc.repr()), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ) + } + })), + }; + match a { + Object::Harita(map) => { + let tok = self.program.get(self.current + 1).unwrap(); + match tok.typ { + TokenType::Identifier { ref id } => { + let o = match map.map.get(id) { + Some(o) => o.clone(), + None => return Err((stack, hashs.clone(), match get_lang() { + SupportedLanguage::Turkish => ErrorGenerator::error( + "BilinmeyenTanımlayıcı", + &format!( + "bilinmeyen değişken: `{}`, bu değişken bulunamamıştır", + tokenc.repr() + ), + tokenc.line, + tokenc.col, + tokenc.file, + { + let mut hashk = hashs.clone().into_keys(); + hashk.sort(); + let n = hashk.binary_search(id).unwrap_err(); + if hashk.is_empty() { + None + } else { + Some(format!("`{}` demek mi istediniz?", hashk[n])) + } + } + ), + SupportedLanguage::English => ErrorGenerator::error( + "UnknownIdentifier", + &format!( + "unknown identifier: `{}`, this identifier could not be found", + tokenc.repr() + ), + tokenc.line, + tokenc.col, + tokenc.file, + { + let mut hashk = hashs.clone().into_keys(); + hashk.sort(); + let n = hashk.binary_search(id).unwrap_err(); + if hashk.is_empty() { + None + } else { + Some(format!("maybe you meant {}?", hashk[n])) + } + }, + ), + })), + }; + latest_id = Some(id); + stack.push(o); + self.current += 2; + }, + TokenType::Çarpı => { + for (k, v) in map.map.iter() { + hashs.insert(k.clone(), v.clone()); + } + let next = match self.program.get(self.current + 2) { + Some(t) => t, + None => todo!(), // Error + }; + match next.typ { + TokenType::InScopeParentR => { + self.current += 3; + break; + }, + _ => todo!(), // Error + } + }, + _ => todo!(), // Error + }; + } + b => return Err(( + stack, + hashs, + match get_lang() { + SupportedLanguage::Turkish => ErrorGenerator::error( + "BeklenmedikTip", + &format!("harita beklenmişti ancak `{:?}` bulundu", b), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ), + SupportedLanguage::English => ErrorGenerator::error( + "UnexpectedType", + &format!("expected map but found `{:?}`", b), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ), + }, + )), + } + } + TokenType::InScopeParentR => { + self.current += 1; + if let Some(id) = latest_id { + hashs.insert(id.clone(), stack.pop().unwrap()); + } else { unreachable!() } + break; + } + _ => todo!(), + } + } + stack.del_stack(); + } + TokenType::InScopeParentR => {}, TokenType::İkiNokta => { let a = match stack.pop() { Some(a) => a, diff --git a/src/store.rs b/src/store.rs index 91f24d8..028d53b 100644 --- a/src/store.rs +++ b/src/store.rs @@ -4,4 +4,3 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub mod globarg { pub static mut SUPRESS_WARN: bool = false; } - diff --git a/src/token/tokens.rs b/src/token/tokens.rs index c812c3f..2f6aa0c 100644 --- a/src/token/tokens.rs +++ b/src/token/tokens.rs @@ -47,6 +47,8 @@ pub mod tokentypes { Hiç, Blok, BlokSonlandır, + InScopeParentL, + InScopeParentR, EOF, } @@ -103,6 +105,8 @@ pub mod tokentypes { Tipinde, Hiç, Blok, + InScopeParentL, + InScopeParentR, EOF, } } @@ -217,6 +221,8 @@ impl ParserToken { TokTyp::Blok => "blok".to_string(), TokTyp::BlokSonlandır => "son".to_string(), TokTyp::İkiNokta => ":".to_string(), + TokTyp::InScopeParentL => "(".to_string(), + TokTyp::InScopeParentR => ")".to_string(), } } } From 2f61748293ced2dea6faa56f9a0cf1d0d216a12c Mon Sep 17 00:00:00 2001 From: kaiserthe13th Date: Sat, 5 Feb 2022 15:30:34 +0300 Subject: [PATCH 2/3] fixed errors as well --- src/runtime/runtime.rs | 87 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/src/runtime/runtime.rs b/src/runtime/runtime.rs index 281d68f..5bc52f8 100644 --- a/src/runtime/runtime.rs +++ b/src/runtime/runtime.rs @@ -207,18 +207,97 @@ impl Run { } let next = match self.program.get(self.current + 2) { Some(t) => t, - None => todo!(), // Error + None => return Err(( + stack, + hashs, + match get_lang() { + SupportedLanguage::Turkish => ErrorGenerator::error( + "BeklenmedikSimge", + &format!( + "`)` beklenmişti ancak birşey bulunamadı", + ), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ), + SupportedLanguage::English => ErrorGenerator::error( + "UnexpectedToken", + &format!( + "expected `)`, but found nothing", + ), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ), + }, + )), }; match next.typ { TokenType::InScopeParentR => { self.current += 3; break; }, - _ => todo!(), // Error + _ => return Err(( + stack, + hashs, + match get_lang() { + SupportedLanguage::Turkish => ErrorGenerator::error( + "BeklenmedikSimge", + &format!( + "`)` beklenmişti ancak `{}` bulundu", + next.repr() + ), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ), + SupportedLanguage::English => ErrorGenerator::error( + "UnexpectedToken", + &format!( + "expected `)`, but found `{}`", + next.repr() + ), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ), + }, + )), } }, - _ => todo!(), // Error - }; + _ => return Err(( + stack, + hashs, + match get_lang() { + SupportedLanguage::Turkish => ErrorGenerator::error( + "BeklenmedikSimge", + &format!( + "`)` beklenmişti ancak `{}` bulundu", + tok.repr() + ), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ), + SupportedLanguage::English => ErrorGenerator::error( + "UnexpectedToken", + &format!( + "expected `)`, but found `{}`", + tok.repr() + ), + tokenc.line, + tokenc.col, + tokenc.file, + None, + ), + }, + )), + } } b => return Err(( stack, From e838715008f1f39f389d9e43e3b06c0588a01c3d Mon Sep 17 00:00:00 2001 From: kaiserthe13th Date: Sat, 5 Feb 2022 15:41:27 +0300 Subject: [PATCH 3/3] fixed error msgs for real this time --- src/runtime/runtime.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/runtime/runtime.rs b/src/runtime/runtime.rs index 5bc52f8..6ca34f3 100644 --- a/src/runtime/runtime.rs +++ b/src/runtime/runtime.rs @@ -51,11 +51,11 @@ impl Run { "BilinmeyenTanımlayıcı", &format!( "bilinmeyen değişken: `{}`, bu değişken bulunamamıştır", - tokenc.repr() + tok.repr() ), - tokenc.line, - tokenc.col, - tokenc.file, + tok.line, + tok.col, + tok.file.clone(), { let mut hashk = hashs.clone().into_keys(); hashk.sort(); @@ -69,10 +69,10 @@ impl Run { ), SupportedLanguage::English => ErrorGenerator::error( "UnknownIdentifier", - &format!("unknown identifier: `{}`, this identifier could not be found", tokenc.repr()), - tokenc.line, - tokenc.col, - tokenc.file, + &format!("unknown identifier: `{}`, this identifier could not be found", tok.repr()), + tok.line, + tok.col, + tok.file.clone(), { let mut hashk = hashs.clone().into_keys(); hashk.sort(); @@ -159,13 +159,13 @@ impl Run { "BilinmeyenTanımlayıcı", &format!( "bilinmeyen değişken: `{}`, bu değişken bulunamamıştır", - tokenc.repr() + tok.repr() ), - tokenc.line, - tokenc.col, - tokenc.file, + tok.line, + tok.col, + tok.file.clone(), { - let mut hashk = hashs.clone().into_keys(); + let mut hashk: Vec<_> = map.map.clone().into_keys().collect(); hashk.sort(); let n = hashk.binary_search(id).unwrap_err(); if hashk.is_empty() { @@ -179,13 +179,13 @@ impl Run { "UnknownIdentifier", &format!( "unknown identifier: `{}`, this identifier could not be found", - tokenc.repr() + tok.repr() ), - tokenc.line, - tokenc.col, - tokenc.file, + tok.line, + tok.col, + tok.file.clone(), { - let mut hashk = hashs.clone().into_keys(); + let mut hashk: Vec<_> = map.map.clone().into_keys().collect(); hashk.sort(); let n = hashk.binary_search(id).unwrap_err(); if hashk.is_empty() {