diff --git a/README.md b/README.md
index 5b78888..b0c85a2 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@
Typesharp is a typesafe compiled language written in rustlang aiming to memory safe and fast!
You can view the [documentation here](https://github.com/TypeSharp/Documentation).
-**NOTICE:** This is a rewrite of the original compiler, and therefore is public. I will not explain how to use, or interact with this source code until I am comfortable releasing the CLI.
+**NOTICE:** This is a rewrite of the original compiler, and therefore is public.
## Why Typesharp?
diff --git a/src/compiler/ReadMe.md b/src/compiler/ReadMe.md
index c3e3dc1..b2fcdde 100644
--- a/src/compiler/ReadMe.md
+++ b/src/compiler/ReadMe.md
@@ -1,5 +1,5 @@
# Compiler - The TypeSharp Compiler.
- - **[typesharp_ast](https://github.com/TypeSharp/Typesharp/tree/master/src/compiler/typesharp_ast)** - Package that contains abstract syntax tree tokens for typesharp.
- - **[typesharp_lexer](https://github.com/TypeSharp/Typesharp/tree/master/src/compiler/typesharp_lexer)** - Lexing utilities, help parse tokens in to AST.
- - **[typesharp_parser](https://github.com/TypeSharp/Typesharp/tree/master/src/compiler/typesharp_parser)** - The typesharp parser is responsible for "parsing" or identifying tokens after `tokenization`.
\ No newline at end of file
+ - **[typesharp_ast](https://github.com/TypeSharp/Typesharp/tree/master/src/compiler/typesharp_ast)** - Package that contains abstract syntax tree tokens for typesharp.
+ - **[typesharp_lexer](https://github.com/TypeSharp/Typesharp/tree/master/src/compiler/typesharp_lexer)** - Lexing utilities, help parse tokens in to AST.
+ - **[typesharp_parser](https://github.com/TypeSharp/Typesharp/tree/master/src/compiler/typesharp_parser)** - The typesharp parser is responsible for "parsing" or identifying tokens after `tokenization`.
\ No newline at end of file
diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs
index 09ae28b..89bbb00 100644
--- a/src/compiler/mod.rs
+++ b/src/compiler/mod.rs
@@ -1,3 +1,3 @@
pub mod typesharp_ast;
pub mod typesharp_lexer;
-pub mod typesharp_parser;
\ No newline at end of file
+pub mod typesharp_parser;
diff --git a/src/compiler/typesharp_ast/Readme.md b/src/compiler/typesharp_ast/Readme.md
index 0432985..7304dd1 100644
--- a/src/compiler/typesharp_ast/Readme.md
+++ b/src/compiler/typesharp_ast/Readme.md
@@ -1,2 +1,2 @@
# typesharp_ast
-Package that contains abstract syntax tree tokens for typesharp.
\ No newline at end of file
+Package that contains abstract syntax tree tokens for typesharp.
diff --git a/src/compiler/typesharp_ast/ast.rs b/src/compiler/typesharp_ast/ast.rs
index 06393eb..97e4c33 100644
--- a/src/compiler/typesharp_ast/ast.rs
+++ b/src/compiler/typesharp_ast/ast.rs
@@ -1,125 +1,57 @@
// Typesharp ast
// use crate::{ typesharp_parser::Module };
-use super::position::{ Position };
-use super::op::*;
use super::types;
-use crate::{ compiler::typesharp_lexer::token::Token };
+use super::util::position;
+use super::node::NodeId;
+use crate::{ compiler::typesharp_parser as parser };
+use crate::compiler::typesharp_lexer::token::Token;
-pub struct AST {
- name: String,
- typ: ProgramType,
- body: Vec
+pub struct Identifier {
+ pub loc: position::Span,
+ pub tokens: Option>
}
-#[derive(Clone, PartialEq, Debug)]
-pub struct IContextResource {
- pub name: String,
- pub contents: Vec
+pub enum StatementKind {
+ Constant(Constant),
+ Item,
+ Expression,
+ Label,
+ Scope,
+ TypeDeclaration(types::Type),
+ Class(Constant)
}
-pub trait IContext {
- /// Initializes a new context
- fn new(&self) -> Self;
-
- // Gets the current resource to be parsed.
- fn getCurrentResource(&self) -> Option;
-
- /// Gets the current AST scope
- fn getCurrentScope(&self) -> Option<&ASTStatement>;
-
- /// Gets all AST scopes
- fn getScopes(&self) -> Vec;
-
- fn nextResource(&self) -> bool;
-}
-
-#[derive(Clone, PartialEq, Debug)]
-pub struct AnyContext;
-
-impl AnyContext {
- fn new(&self) -> AnyContext {
- return Self;
- }
-
- fn getLine() -> u8 {
- 0
- }
+pub enum ExpressionKind {
+ /// An array of any expression
+ Array(Vec),
+ /// Private context
+ Scope,
+ /// A call to a function or reference to a signature
+ Function,
+ /// A method call eg: `foo.bar(a, b)`
+ ///
+ /// The `Identifier` here represents the Name of the method being called
+ /// `Vec` represents the arguments given to the expression
+ Method(Identifier, Vec, position::Span)
}
-pub enum ProgramType {
- /// Production
- PRO,
- /// Library
- LIB,
- /// External Foreign Function Interface
- FFI,
- /// Systematic compile, (NOT COMPILED FOR ANY OS, REQUIRES AN OBJMP)
- SYS,
- /// A driver
- INTERNAL
+pub struct Constant {
+ pub id: NodeId,
+ pub typ: types::Type
}
-pub struct Library;
-
-#[derive(Clone, PartialEq, Debug)]
-pub struct ASTStatement {
- pub body: ASTStateBody,
- pub context: Context,
- pub pos: Position
+pub struct Statement {
+ pub id: NodeId,
+ pub kind: StatementKind,
+ pub loc: position::Span
}
-#[derive(Clone, PartialEq, Debug)]
-pub enum ASTStateBody {
- // expressions, function calls, returns etc should be here.
- FuncCall(Signature),
- Expression(Expression),
- StackVar(Var),
- Constant(HeapVar),
- AnyConstant(AnyVar),
- If(Conditional)
+pub struct Param {
+ pub id: NodeId,
+ // pub val: Node
}
-// Context and definitions
-/// A variable, const, class, etc.
-#[derive(Clone, PartialEq, Debug)]
-pub struct Var {
- pub op: Option,
- pub typ: Option,
- pub val: Token,
- pub pos: Position,
- pub dies: bool,
- // pub typ: Type
-}
-
-/// Dynamic variable, extends var, which is static.
-#[derive(Clone, PartialEq, Debug)]
-pub struct HeapVar {
- pub var: Var,
- pub mangled: bool
-}
-
-#[derive(Clone, PartialEq, Debug)]
-pub enum AnyVar {
- Static(Var),
- Heap(HeapVar)
-}
-
-/// Functions
-#[derive(Clone, PartialEq, Debug)]
-pub struct Signature {
- pub name: Var,
- pub dynamic: bool
-}
-
-#[derive(Clone, PartialEq, Debug)]
pub struct Expression {
- pub ops: Vec,
- pub v: Vec
-}
-
-#[derive(Clone, PartialEq, Debug)]
-pub struct Conditional {
- pub condition: Expression,
- pub body: Vec,
- pub fin: Option>
+ pub id: NodeId,
+ pub kind: ExpressionKind,
}
\ No newline at end of file
diff --git a/src/compiler/typesharp_ast/cursor.rs b/src/compiler/typesharp_ast/cursor.rs
deleted file mode 100644
index 63342c8..0000000
--- a/src/compiler/typesharp_ast/cursor.rs
+++ /dev/null
@@ -1,91 +0,0 @@
-use std::str::Chars;
-use super::position::Position;
-
-pub struct Cursor<'a> {
- init_length: usize,
- chars: Chars<'a>,
- pub previous: char,
- pub pos: Position
-}
-
-pub const EOF: char = '\0';
-
-impl<'a> Cursor<'a> {
- /// Creates a new cursor.
- /// Very useful for creating.
- pub fn new(input: &'a str) -> Cursor<'a> {
- Cursor {
- init_length: input.len(),
- chars: input.chars(),
- previous: EOF,
- pos: Position::new(0, 0)
- }
- }
-
- /// Increases the cursor offset, consumes the next character
- /// Do not use if you are trying to "check" a offset in advance.
- pub fn peek(&mut self) -> Option {
- let c: char = self.chars.next()?;
-
- self.previous = c;
- self.pos.increment(c);
-
- return Some(c);
- }
-
- /// Decreases the cursor offset.
- /// Keep in mind, if the character has already been consumed
- /// EOF is returned
- pub fn unpeek(&mut self) -> char {
- return self.chars.nth(self.length_consumed() - 1).unwrap_or(EOF);
- }
-
- // Grabs the next char without consuming it.
- pub fn first(&self) -> char {
- return self.nth_char(0);
- }
-
- // Grabs the second char without consuming it.
- pub fn second(&self) -> char {
- return self.nth_char(1);
- }
-
- /// Returns the `nth_char` releative to the current cursor pos
- /// If the position given doesn't exist, `EOF` is returned.
- pub fn nth_char(&self, amt: usize) -> char {
- return self.chars().nth(amt).unwrap_or(EOF);
- }
-
- /// Copies the current chars in the cursor.
- pub fn chars(&self) -> Chars<'a> {
- return self.chars.clone();
- }
-
- /// Checks the length that has been consumed by the cursor
- /// Consumed symbols are not kept.
- pub fn length_consumed(&self) -> usize {
- return self.init_length - self.chars.as_str().len();
- }
-
- /// Checks whether or not if theres more chars to consume
- /// Returns true, if all chars have been consumed.
- pub fn is_eof(&self) -> bool {
- return self.chars.as_str().is_empty();
- }
-
- /// Consumes chars until the predicate returns false or the end of file is met.
- pub fn consume_while(&mut self, mut pred: impl FnMut(char) -> bool) {
- while !self.is_eof() && pred(self.first()) {
- self.peek();
- }
- }
-
- /// Consumes chars until the predicate returns false, or until the end of file is met, and returns the offspring.
- pub fn consume_segment(&mut self, mut pred: impl FnMut(char) -> bool) -> String {
- let mut segment = String::new();
- while !self.is_eof() && pred(self.first()) == true {
- segment.push(self.peek().unwrap());
- }
- return segment;
- }
-}
\ No newline at end of file
diff --git a/src/compiler/typesharp_ast/keyword.rs b/src/compiler/typesharp_ast/keyword/mod.rs
similarity index 97%
rename from src/compiler/typesharp_ast/keyword.rs
rename to src/compiler/typesharp_ast/keyword/mod.rs
index 37ab829..f97bed8 100644
--- a/src/compiler/typesharp_ast/keyword.rs
+++ b/src/compiler/typesharp_ast/keyword/mod.rs
@@ -1,3 +1,4 @@
+use crate::compiler::typesharp_lexer::token::TokenValue;
use std::{error, fmt};
/// These keywords are reserved.
@@ -318,6 +319,12 @@ impl KeyWord {
}
}
+impl TokenValue for KeyWord {
+ fn get(&self) -> String {
+ String::from(self.as_str())
+ }
+}
+
impl From<&'static str> for KeyWord {
fn from(m: &'static str) -> KeyWord {
return KeyWord::from_str(m);
@@ -345,4 +352,4 @@ impl fmt::Display for KeyWord {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
return fmt::Display::fmt(self.as_str(), f);
}
-}
\ No newline at end of file
+}
diff --git a/src/compiler/typesharp_ast/mod.rs b/src/compiler/typesharp_ast/mod.rs
index e25fd6c..2c7b2c5 100644
--- a/src/compiler/typesharp_ast/mod.rs
+++ b/src/compiler/typesharp_ast/mod.rs
@@ -1,12 +1,11 @@
pub mod ast;
-pub mod position;
pub mod keyword;
-pub mod cursor;
-pub mod op;
pub mod types;
+pub mod util;
+pub mod node;
pub use self::{
- position::{ Position, Span },
- keyword::{ KeyWord, KeyWordError },
- cursor::{ Cursor }
-};
\ No newline at end of file
+ keyword::{KeyWord, KeyWordError},
+ util::cursor::Cursor,
+ util::position::{Position, Span},
+};
diff --git a/src/compiler/typesharp_ast/node/mod.rs b/src/compiler/typesharp_ast/node/mod.rs
new file mode 100644
index 0000000..a787fff
--- /dev/null
+++ b/src/compiler/typesharp_ast/node/mod.rs
@@ -0,0 +1,7 @@
+pub struct NodeId {
+ id: u16
+}
+
+pub struct Node {
+
+}
\ No newline at end of file
diff --git a/src/compiler/typesharp_ast/op.rs b/src/compiler/typesharp_ast/op.rs
deleted file mode 100644
index 870aee2..0000000
--- a/src/compiler/typesharp_ast/op.rs
+++ /dev/null
@@ -1,147 +0,0 @@
-#[derive(Clone, PartialEq, Debug)]
-pub enum AnyOp {
- BinOp(BinOp),
- UnaryOp(UnaryOp),
- LogicalOp(LogicalOp),
- ComparisonOp(ComparisonOp),
- AssignmentOp(AssignmentOp)
-}
-
-// Binary Operators
-#[derive(Clone, PartialEq, Debug)]
-pub enum BinOp {
- // +
- Plus,
-
- // -
- Minus,
-
- // *
- Star,
-
- // /
- Slash,
-
- // %
- Percent,
-
- // ^
- Caret,
-
- // &
- And,
-
- // |
- Or,
-
- // <<
- Sh1,
-
- // >>
- Shr,
-
- // >>>
- UShr,
-}
-
-#[derive(Clone, PartialEq, Debug)]
-pub enum UnaryOp {
- // ++x
- IncP,
-
- // x++
- Inc,
-
- // --x
- DecP,
-
- // x--
- Dec,
-
- // -x
- Neg,
-
- // +x
- Pos,
-
- // !x
- Not,
-
- // experimental delete x
- Delete,
-
- // A syntax sugar for x = {}
- Object,
-}
-
-#[derive(Clone, PartialEq, Debug)]
-pub enum LogicalOp {
- // x && y
- And,
-
- // x || y
- Or,
-
- // x ?? y
- Coalasce,
-}
-
-#[derive(Clone, PartialEq, Debug)]
-pub enum ComparisonOp {
- Eq,
-
- NotEq,
-
- GreaterThan,
-
- GreaterThanOrEqual,
-
- LessThan,
-
- LessThanOrEqual,
-
- Contains,
-
- In,
-
- InstanceOf,
-}
-
-#[derive(Clone, PartialEq, Debug)]
-pub enum AssignmentOp {
- // x += y
- Add,
-
- // x -= y
- Sub,
-
- // x *= y
- Mul,
-
- // x /= y
- Div,
-
- // x %= y
- Rem,
-
- And,
-
- Or,
-
- Xor,
-
- Sh1,
-
- Shr,
-
- Ushr,
-
- // [EXPERIMENT] x &&= y
- BoolAnd,
-
- // [EXPERIMENT] x ||= y
- BoolOr,
-
- // [EXPERIMENT] x ??= y : Support may not be in future versions
- Coalesce,
-}
\ No newline at end of file
diff --git a/src/compiler/typesharp_ast/position.rs b/src/compiler/typesharp_ast/position.rs
deleted file mode 100644
index 8b3433d..0000000
--- a/src/compiler/typesharp_ast/position.rs
+++ /dev/null
@@ -1,100 +0,0 @@
-use std::{ fmt, cmp::Ordering };
-use super::cursor::EOF;
-
-///
-/// This is the position that is lexed.
-/// Tokens will contain a specific position, as well as lines.
-///
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
-pub struct Position {
- line: u32,
- column: u32
-}
-
-impl Position {
- #[inline]
- #[track_caller]
- pub fn new(line: u32, column: u32) -> Self {
- return Self {
- line: line,
- column: column
- }
- }
-
- pub fn increment(&mut self, c: char) -> bool {
- if c == '\n' {
- self.line = self.line + 1;
- return true;
- } else if c == EOF {
- return false;
- } else {
- self.column = self.column + 1;
- return true;
- }
- }
-}
-
-impl fmt::Display for Position {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- return write!(f, "{}:{}", self.line, self.column);
- }
-}
-
-/// This is used for grabbing a "section" or "span"
-/// of code in a file. EG: Comments.
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord)]
-pub struct Span {
- start: Position,
- end: Position
-}
-
-impl Span {
- pub fn new(start: Position, end: Position) -> Self {
- assert!(start <= end, "Start position can not be after end position.");
-
- return Self { start: start, end: end };
- }
-
- pub fn contains(self, other: Span) -> bool {
- return self.start <= other.start && self.end >= other.end;
- }
-
- pub fn into_position(&self) -> Position {
- return self.start.clone();
- }
-
- // to-do: Shrink
-
- pub fn verify(self) -> bool {
- return self.start <= self.end;
- }
-}
-
-impl fmt::Display for Span {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- return write!(f, "Span {{ {}, {} }}", self.start, self.end);
- }
-}
-
-impl From for Span {
- fn from(pos: Position) -> Self {
- Self {
- start: pos,
- end: pos
- }
- }
-}
-
-impl PartialOrd for Span {
- fn partial_cmp(&self, other: &Self) -> Option {
- if self == other {
- return Some(Ordering::Equal);
- } else if self.end < other.start {
- return Some(Ordering::Less);
- } else if self.start > other.end {
- return Some(Ordering::Greater);
- } else {
- return None;
- }
- }
-}
\ No newline at end of file
diff --git a/src/compiler/typesharp_ast/types/builtin.rs b/src/compiler/typesharp_ast/types/builtin.rs
deleted file mode 100644
index 30b7119..0000000
--- a/src/compiler/typesharp_ast/types/builtin.rs
+++ /dev/null
@@ -1 +0,0 @@
-// this is used by the parser
\ No newline at end of file
diff --git a/src/compiler/typesharp_ast/types/compiler.rs b/src/compiler/typesharp_ast/types/compiler.rs
deleted file mode 100644
index 30b7119..0000000
--- a/src/compiler/typesharp_ast/types/compiler.rs
+++ /dev/null
@@ -1 +0,0 @@
-// this is used by the parser
\ No newline at end of file
diff --git a/src/compiler/typesharp_ast/types/mod.rs b/src/compiler/typesharp_ast/types/mod.rs
index 24cb304..13a4bb4 100644
--- a/src/compiler/typesharp_ast/types/mod.rs
+++ b/src/compiler/typesharp_ast/types/mod.rs
@@ -1,22 +1,13 @@
// This is a module that handles internal classes and types.
-pub mod builtin;
-pub mod compiler;
-use super::{ ast };
+use crate::{ compiler::typesharp_lexer::Token };
-#[derive(Clone, PartialEq, Debug)]
pub struct Type {
- pub name: String,
- pub statement: TypeDefinition
+ pub kind: TypeKinds,
+ pub tokens: Option>
}
-#[derive(Clone, PartialEq, Debug)]
-pub struct TypeDefinition {
- /// Used for linting and more descriptive errors
- pub calculated_defs: Option>,
- pub defs: Option>
-}
-
-pub use self::{
- builtin::*,
- compiler::*
-};
\ No newline at end of file
+pub enum TypeKinds {
+ RawPtr,
+ AClass,
+ ARef
+}
\ No newline at end of file
diff --git a/src/compiler/typesharp_ast/util/cursor.rs b/src/compiler/typesharp_ast/util/cursor.rs
new file mode 100644
index 0000000..83c285e
--- /dev/null
+++ b/src/compiler/typesharp_ast/util/cursor.rs
@@ -0,0 +1,91 @@
+use super::position::Position;
+use std::str::Chars;
+
+pub struct Cursor<'a> {
+ init_length: usize,
+ chars: Chars<'a>,
+ pub previous: char,
+ pub pos: Position,
+}
+
+pub const EOF: char = '\0';
+
+impl<'a> Cursor<'a> {
+ /// Creates a new cursor.
+ /// Very useful for creating.
+ pub fn new(input: &'a str) -> Cursor<'a> {
+ Cursor {
+ init_length: input.len(),
+ chars: input.chars(),
+ previous: EOF,
+ pos: Position::new(0, 0),
+ }
+ }
+
+ /// Increases the cursor offset, consumes the next character
+ /// Do not use if you are trying to "check" a offset in advance.
+ pub fn peek(&mut self) -> Option {
+ let c: char = self.chars.next()?;
+
+ self.previous = c;
+ self.pos.increment(c);
+
+ return Some(c);
+ }
+
+ /// Decreases the cursor offset.
+ /// Keep in mind, if the character has already been consumed
+ /// EOF is returned
+ pub fn unpeek(&mut self) -> char {
+ return self.chars.nth(self.length_consumed() - 1).unwrap_or(EOF);
+ }
+
+ // Grabs the next char without consuming it.
+ pub fn first(&self) -> char {
+ return self.nth_char(0);
+ }
+
+ // Grabs the second char without consuming it.
+ pub fn second(&self) -> char {
+ return self.nth_char(1);
+ }
+
+ /// Returns the `nth_char` releative to the current cursor pos
+ /// If the position given doesn't exist, `EOF` is returned.
+ pub fn nth_char(&self, amt: usize) -> char {
+ return self.chars().nth(amt).unwrap_or(EOF);
+ }
+
+ /// Copies the current chars in the cursor.
+ pub fn chars(&self) -> Chars<'a> {
+ return self.chars.clone();
+ }
+
+ /// Checks the length that has been consumed by the cursor
+ /// Consumed symbols are not kept.
+ pub fn length_consumed(&self) -> usize {
+ return self.init_length - self.chars.as_str().len();
+ }
+
+ /// Checks whether or not if theres more chars to consume
+ /// Returns true, if all chars have been consumed.
+ pub fn is_eof(&self) -> bool {
+ return self.chars.as_str().is_empty();
+ }
+
+ /// Consumes chars until the predicate returns false or the end of file is met.
+ pub fn consume_while(&mut self, mut pred: impl FnMut(char) -> bool) {
+ while !self.is_eof() && pred(self.first()) {
+ self.peek();
+ }
+ }
+
+ /// Consumes chars until the predicate returns false, or until the end of file is met, and returns the offspring.
+ pub fn consume_segment(&mut self, mut pred: impl FnMut(char) -> bool) -> String {
+ let mut segment = String::new();
+ while !self.is_eof() && pred(self.first()) == true {
+ segment.push(self.peek().unwrap());
+ }
+ return segment;
+ }
+}
diff --git a/src/compiler/typesharp_ast/util/mod.rs b/src/compiler/typesharp_ast/util/mod.rs
new file mode 100644
index 0000000..1b1c585
--- /dev/null
+++ b/src/compiler/typesharp_ast/util/mod.rs
@@ -0,0 +1,2 @@
+pub mod cursor;
+pub mod position;
diff --git a/src/compiler/typesharp_ast/util/position.rs b/src/compiler/typesharp_ast/util/position.rs
new file mode 100644
index 0000000..f3b3922
--- /dev/null
+++ b/src/compiler/typesharp_ast/util/position.rs
@@ -0,0 +1,106 @@
+use super::cursor::EOF;
+use std::{cmp::Ordering, fmt};
+
+///
+/// This is the position that is lexed.
+/// Tokens will contain a specific position, as well as lines.
+///
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
+pub struct Position {
+ line: u32,
+ column: u32,
+}
+
+impl Position {
+ #[inline]
+ #[track_caller]
+ pub fn new(line: u32, column: u32) -> Self {
+ return Self {
+ line: line,
+ column: column,
+ };
+ }
+
+ pub fn increment(&mut self, c: char) -> bool {
+ if c == '\n' {
+ self.line = self.line + 1;
+ return true;
+ } else if c == EOF {
+ return false;
+ } else {
+ self.column = self.column + 1;
+ return true;
+ }
+ }
+}
+
+impl fmt::Display for Position {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ return write!(f, "{}:{}", self.line, self.column);
+ }
+}
+
+/// This is used for grabbing a "section" or "span"
+/// of code in a file. EG: Comments.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord)]
+pub struct Span {
+ start: Position,
+ end: Position,
+}
+
+impl Span {
+ pub fn new(start: Position, end: Position) -> Self {
+ assert!(
+ start <= end,
+ "Start position can not be after end position."
+ );
+
+ return Self {
+ start: start,
+ end: end,
+ };
+ }
+
+ pub fn contains(self, other: Span) -> bool {
+ return self.start <= other.start && self.end >= other.end;
+ }
+
+ pub fn into_position(&self) -> Position {
+ return self.start.clone();
+ }
+
+ // to-do: Shrink
+
+ pub fn verify(self) -> bool {
+ return self.start <= self.end;
+ }
+}
+
+impl fmt::Display for Span {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ return write!(f, "Span {{ {}, {} }}", self.start, self.end);
+ }
+}
+
+impl From for Span {
+ fn from(pos: Position) -> Self {
+ Self {
+ start: pos,
+ end: pos,
+ }
+ }
+}
+
+impl PartialOrd for Span {
+ fn partial_cmp(&self, other: &Self) -> Option {
+ if self == other {
+ return Some(Ordering::Equal);
+ } else if self.end < other.start {
+ return Some(Ordering::Less);
+ } else if self.start > other.end {
+ return Some(Ordering::Greater);
+ } else {
+ return None;
+ }
+ }
+}
diff --git a/src/compiler/typesharp_lexer/Readme.md b/src/compiler/typesharp_lexer/Readme.md
index f7375d7..11658af 100644
--- a/src/compiler/typesharp_lexer/Readme.md
+++ b/src/compiler/typesharp_lexer/Readme.md
@@ -1,2 +1,2 @@
# typesharp_lexer
-Lexing utilities, help parse tokens in to AST.
\ No newline at end of file
+Lexing utilities, help parse tokens in to AST.
diff --git a/src/compiler/typesharp_lexer/mod.rs b/src/compiler/typesharp_lexer/mod.rs
index 057d96c..5a01599 100644
--- a/src/compiler/typesharp_lexer/mod.rs
+++ b/src/compiler/typesharp_lexer/mod.rs
@@ -1,5 +1,3 @@
pub mod token;
-pub use self::{
- token::{ Token, TokenKind, tokenize },
-};
\ No newline at end of file
+pub use self::token::{tokenize, Token, TokenKind};
diff --git a/src/compiler/typesharp_lexer/token.rs b/src/compiler/typesharp_lexer/token.rs
index e27c9bb..72f850f 100644
--- a/src/compiler/typesharp_lexer/token.rs
+++ b/src/compiler/typesharp_lexer/token.rs
@@ -1,65 +1,162 @@
use crate::{
- compiler::typesharp_ast::{ Span, Position, KeyWord, Cursor, op::* }
+ compiler::typesharp_ast::{Cursor, KeyWord, Position, Span},
+ compiler::typesharp_parser::op::*,
};
+pub type TokenType = (String, Box);
#[derive(Clone, PartialEq, Debug)]
pub struct Token {
- pub kind: TokenKind,
- pub span: Span,
- pub position: Position
+ pub kind: TokenKind,
+ pub span: Span,
+ pub position: Position,
}
+impl Token {
+ pub fn new(kind: TokenKind, span: Span, pos: Option) -> Self {
+ return Token {
+ kind: kind,
+ span: span,
+ position: pos.unwrap_or(Position::new(0, 0)),
+ };
+ }
+
+ pub fn build(kind: TokenKind, pos: Position) -> Self {
+ return Token::new(kind, Span::from(pos), Some(pos));
+ }
+}
+
+/// Public trait
+/// This should be used to extract a value from a TokenKind (or related)
+/// By default, typesharp **relies** on this implementation during parsing.
+pub trait TokenValue {
+ fn get(&self) -> T;
+}
+
+/// Numerics!
+/// Any numeric type can be referred to as below
#[derive(Clone, PartialEq, Debug)]
pub enum Numeric {
- /// @reference https://doc.rust-lang.org/beta/reference/types/numeric.html
- /// Floating number
- FloatLiteral(f32),
+ /// @reference https://doc.rust-lang.org/beta/reference/types/numeric.html
+ /// Floating number
+ FloatLiteral(f32),
- /// Floating number that is larger than a float
- DoubleLiteral(f64),
+ /// Floating number that is larger than a float
+ DoubleLiteral(f64),
- /// Integer: const blah: int = 13910;
- IntegerLiteral(i32),
+ /// Integer: const blah: int = 13910;
+ IntegerLiteral(i32),
- /// probably going to allow types for: i64 numerically. Compiler will detect?
- IntegerLiteralBig(i64),
+ /// probably going to allow types for: i64 numerically. Compiler will detect?
+ IntegerLiteralBig(i64),
- /// idk wtf you would need this for but, its there lmfao
- ItegerLiteralSigned128(i128),
+ /// idk wtf you would need this for but, its there lmfao
+ ItegerLiteralSigned128(i128),
- /// Any number that starts with: "0b".
- Binary,
+ /// Any number that starts with: "0b".
+ Binary(usize),
- /// Any number that starts with: "0o".
- Octal,
+ /// Any number that starts with: "0o".
+ Octal(usize),
- /// Any number that starts with: "0x".
- Hexadecimal,
+ /// Any number that starts with: "0x".
+ Hexadecimal(usize),
}
+impl Numeric {
+ fn new(s: String) -> Self {
+ match s {
+ _ => Numeric::Hexadecimal(0),
+ }
+ }
+}
+
+impl TokenValue for Numeric {
+ fn get(&self) -> usize {
+ match *self {
+ Numeric::Binary(n) => n,
+ Numeric::FloatLiteral(n) => n as usize,
+ Numeric::Hexadecimal(n) => n,
+ Numeric::IntegerLiteral(n) => n as usize,
+ Numeric::IntegerLiteralBig(n) => n as usize,
+ Numeric::ItegerLiteralSigned128(n) => n as usize,
+ Numeric::Octal(n) => n,
+ _ => panic!("Unknown Numeric Type provided."),
+ }
+ }
+}
+
+impl std::fmt::Display for Numeric {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "0") // lazy impl, todo: properly impl
+ }
+}
+
+/// Comment Implementation
+///
+/// Block Comments:
+/// /* test */
+///
+/// Line Comments:
+/// // test
#[derive(Clone, PartialEq, Debug)]
pub enum Comment {
- /// Line of the comment
- Line(Span),
+ /// Line of the comment
+ Line(String),
- /// Block
- Block(String),
+ /// Block
+ Block(String),
+}
+
+impl TokenValue for Comment {
+ fn get(&self) -> String {
+ match self {
+ Comment::Line(c) => c.to_string(),
+ Comment::Block(c) => c.to_string()
+ }
+ }
}
+impl std::fmt::Display for Comment {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ Comment::Line(c) => write!(f, "Comment {{ {} }}", c),
+ Comment::Block(c) => write!(f, "Comment {{ {} }}", c)
+ }
+ }
+}
+
+/// Delimiters are defined below, each delimiter is a type of scope delarator
#[derive(Clone, PartialEq, Debug)]
pub enum Delimiter {
- /// Parenthesis, either "(" or ")"
- Paren,
+ /// Parenthesis, either "(" or ")"
+ Paren(String),
+
+ /// Bracket, either "[" or "]"
+ Bracket(String),
- /// Bracket, either "[" or "]"
- Bracket,
+ /// Brace, either "{" or "}"
+ Brace(String),
+
+ /// No delimiter
+ NoDelim,
+}
- /// Brace, either "{" or "}"
- Brace,
+impl TokenValue for Delimiter {
+ fn get(&self) -> String {
+ match self {
+ Delimiter::Paren(t) => t.to_string(),
+ Delimiter::Bracket(t) => t.to_string(),
+ Delimiter::NoDelim => String::from("None"),
+ _ => panic!("Unknown Delimiter"),
+ }
+ }
+}
- /// No delimiter
- NoDelim,
+impl std::fmt::Display for Delimiter {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ return write!(f, "Delimiter {{ {} }}", self.get());
+ }
}
#[derive(Clone, PartialEq, Debug)]
@@ -68,47 +165,46 @@ pub enum TokenKind {
// EG: "."
Accessor,
- // A boolean, true or false
- BoolLiteral(Box),
+ // A boolean, true or false
+ BoolLiteral(String),
- // End of file
- EOF,
+ // End of file
+ EOF,
Keyword(KeyWord),
- // A identifier (const x = 0) where x is the identifier
- Identifier(String),
+ // A identifier (const x = 0) where x is the identifier
+ Identifier(String),
- //Keyword(Keyword),
+ //Keyword(Keyword),
- // A string, literal. EG: Constant
- StringLiteral(String),
+ // A string, literal. EG: Constant
+ StringLiteral(String),
- // No idea on how I want to do this yet
- ErrorLiteral,
+ // No idea on how I want to do this yet
+ ErrorLiteral,
- NumberLiteral(Box),
+ NumberLiteral(Numeric),
- //RegularExpressionLiteral,
+ //RegularExpressionLiteral,
- // A template string, wrapped in ``
- TemplateLiteral(Box),
+ // A template string, wrapped in ``
+ TemplateLiteral(String),
- // Comment literal
- CommentLiteral(Comment),
+ // Comment literal
+ CommentLiteral(Comment),
// Using this until i get comments situated.
- Comment,
+ // Comment,
+ DelimiterLiteral(Delimiter),
- DelimiterLiteral(Delimiter),
+ BinaryOpLiteral(BinOp),
- BinaryOpLiteral(BinOp),
+ UnaryOpLiteral(UnaryOp),
- UnaryOpLiteral(UnaryOp),
+ GenericType(String),
- GenericType(Box),
-
- AssignmentLiteral(AssignmentOp),
+ AssignmentLiteral(AssignmentOp),
ExpressionTerminator,
@@ -117,16 +213,44 @@ pub enum TokenKind {
WhiteSpace,
// Unknown token not expected by our lexer
- Unknown(String)
+ Unknown(String),
}
-impl Token {
- pub fn new(kind: TokenKind, span: Span, pos: Option) -> Self {
- return Token { kind: kind, span: span, position: pos.unwrap_or(Position::new(0, 0)) }
- }
+impl TokenKind {
+ /// Gets the token as a string value
+ fn as_str(&self) -> String {
+ match self {
+ TokenKind::Accessor => String::from("."),
+ TokenKind::BoolLiteral(v) => String::from(v),
+ TokenKind::EOF => String::from("EOF"),
+ TokenKind::Keyword(v) => v.get(),
+ TokenKind::Identifier(v) => v.to_string(),
+ TokenKind::StringLiteral(v) => v.to_string(),
+ TokenKind::ErrorLiteral => String::from("Error"),
+ TokenKind::NumberLiteral(n) => format!("{}", n.get()),
+ TokenKind::TemplateLiteral(v) => v.to_string(),
+ TokenKind::CommentLiteral(c) => c.get(),
+ TokenKind::DelimiterLiteral(v) => v.get(),
+ TokenKind::BinaryOpLiteral(_v) => String::from("Op unknown"),
+ TokenKind::UnaryOpLiteral(_v) => String::from("Op unknown"),
+ TokenKind::GenericType(_v) => String::from("Op unknown"),
+ TokenKind::AssignmentLiteral(_v) => String::from("Op unknown"),
+ TokenKind::ExpressionTerminator => String::from("Expression Terminated"),
+ TokenKind::Indent => String::from(""),
+ TokenKind::WhiteSpace => String::from(" "),
+ TokenKind::Unknown(v) => v.to_string()
+ }
+ }
+}
- pub fn build(kind: TokenKind, pos: Position) -> Self {
- return Token::new(kind, Span::from(pos), Some(pos));
+impl std::fmt::Display for TokenKind {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match *self {
+ TokenKind::CommentLiteral(_) => write!(f, "CommentLiteral"),
+ TokenKind::NumberLiteral(_) => write!(f, "Number"),
+ TokenKind::Indent | TokenKind::WhiteSpace => write!(f, "Space or Whitespace"),
+ _ => write!(f, "Unknown token."),
+ }
}
}
@@ -141,18 +265,6 @@ macro_rules! token {
};
}
-impl std::fmt::Display for TokenKind {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match *self {
- TokenKind::CommentLiteral(_) => write!(f, "CommentLiteral"),
- TokenKind::Comment => write!(f, "Comment"),
- TokenKind::NumberLiteral(_) => write!(f, "Number"),
- TokenKind::Indent | TokenKind::WhiteSpace => write!(f, "Space or Whitespace"),
- _ => write!(f, "Unknown token.")
- }
- }
-}
-
// I don't want to do this lallalalaala
// impl From<&'static str> for Token {
//
@@ -167,37 +279,59 @@ impl Cursor<'_> {
'/' => match self.first() {
'/' => self.consume_comment(true),
'*' => self.consume_comment(false),
- _ => Token::new(TokenKind::BinaryOpLiteral(BinOp::Slash), Span::from(self.pos), None)// probably an op
+ _ => Token::new(
+ TokenKind::BinaryOpLiteral(BinOp::Slash),
+ Span::from(self.pos),
+ None,
+ ), // probably an op
},
// numbers (parser checks for numeric types later)
'0'..='9' => self.consume_any_numeric(*init),
// whitespace (eg: space)
- ' '|'\n'|'\r'|'\t' => Token::new(TokenKind::WhiteSpace, Span::from(self.pos), None),
+ ' ' | '\n' | '\r' | '\t' => {
+ Token::new(TokenKind::WhiteSpace, Span::from(self.pos), None)
+ },
+ '"' | '\'' => self.consume_any_string(Some(init)),
'A'..='z' => self.consume_keyword_or_identifier(Some(init)),
- '.' => panic!("Unknown"),
+ '.' => token!(TokenKind::Accessor, Span::from(self.pos)),
';' => token!(TokenKind::ExpressionTerminator, Span::from(self.pos)),
- _ => token!(TokenKind::Unknown(init.to_string()), Span::from(self.pos))
+ _ => token!(TokenKind::Unknown(init.to_string()), Span::from(self.pos)),
};
}
/// Indefinitely consumes any word encapsulated in a string char
pub fn consume_any_string(&mut self, init: Option<&char>) -> Token {
let init_pos: Position = self.pos;
- let mut string: String = String::new();
if init == None {
// there was no initial char
// we need to panic because it's impossible to know when we can terminate the string.
+ // to-do: Implement errors.
panic!("Unknown String");
- } else {
- match *init.unwrap_or(&'\'') {
- _ => panic!("Compiler error.")
- }
}
- return token!(TokenKind::StringLiteral(string), Span::new(init_pos, self.pos));
+ let mut previous = self.previous;
+ let string = self.consume_segment(|c| -> bool {
+ if c == *init.unwrap() {
+ if previous == '\\' {
+ return true;
+ }
+ return false;
+ } else {
+ previous = c;
+ return true;
+ }
+ });
+
+ // consume the next char because it is a string terminator and has not been consumed, (possibly fix cursor?)
+ self.peek();
+
+ return token!(
+ TokenKind::StringLiteral(string),
+ Span::new(init_pos, self.pos)
+ );
}
/// Indefinitely consume until we match whitespace.
@@ -212,7 +346,11 @@ impl Cursor<'_> {
identifier = self.consume_segment(|c| !c.is_whitespace() && c.is_alphanumeric());
} else {
identifier.push(*init.unwrap());
- identifier.push_str(self.consume_segment(|c| !c.is_whitespace() && c.is_alphanumeric()).chars().as_str());
+ identifier.push_str(
+ self.consume_segment(|c| !c.is_whitespace() && c.is_alphanumeric())
+ .chars()
+ .as_str(),
+ );
}
let span: Span = Span::new(init_pos, self.pos);
@@ -289,7 +427,7 @@ impl Cursor<'_> {
"interface" => token!(TokenKind::Keyword(KeyWord::Interface), span),
// Wasn't a keyword, it was an identifier
- _ => token!(TokenKind::Identifier(identifier), span)
+ _ => token!(TokenKind::Identifier(identifier), span),
}
}
@@ -298,15 +436,21 @@ impl Cursor<'_> {
if inline == true {
// consume while
let initpos: Position = self.pos; // maniuplation of this really doesn't affect anything
- self.consume_while(|c| c != '\n');
- return token!(TokenKind::Comment, Span::new(initpos, self.pos));
+ let block: String = self.consume_segment(|c| c != '\n');
+ return token!(
+ TokenKind::CommentLiteral(Comment::Block(block)),
+ Span::new(initpos, self.pos)
+ );
} else {
let initpos: Position = self.pos; // maniuplation of this really doesn't affect anything
self.peek(); // we need this to consume this old char.
- self.consume_while(|c| c != '*');
+ let seg: String = self.consume_segment(|c| c != '*');
self.peek();
self.peek(); // this is hacky, pls find fix
- return token!(TokenKind::Comment, Span::new(initpos, self.pos));
+ return token!(
+ TokenKind::CommentLiteral(Comment::Line(seg)),
+ Span::new(initpos, self.pos)
+ );
}
}
@@ -318,14 +462,26 @@ impl Cursor<'_> {
let mut number: String = String::from(initial);
let init_pos: Position = self.pos;
// immediately check next char but don't consume
- number.push_str(self.consume_segment(|c| c.is_numeric() || c == '.').chars().as_str());
- return Token::new(TokenKind::NumberLiteral(number.into_boxed_str()), Span::new(init_pos, self.pos), None);
+ number.push_str(
+ self.consume_segment(|c| c.is_numeric() || c == '.')
+ .chars()
+ .as_str(),
+ );
+ return Token::new(
+ TokenKind::NumberLiteral(Numeric::new(number)),
+ Span::new(init_pos, self.pos),
+ None,
+ );
+ }
+
+ pub fn last_is_escape_char(&self) -> bool {
+ return self.previous == '\\'
}
}
/// Tokenize an input into a iterator of tokens.
pub fn tokenize(input: &str) -> Vec {
- let mut cursor = Cursor::new(input);
+ let mut cursor = Cursor::new(input);
let mut tokens: Vec = Vec::new();
while !cursor.is_eof() {
@@ -335,4 +491,4 @@ pub fn tokenize(input: &str) -> Vec {
}
return tokens;
-}
\ No newline at end of file
+}
diff --git a/src/compiler/typesharp_parser/Readme.md b/src/compiler/typesharp_parser/Readme.md
index fffca56..6e6e0bc 100644
--- a/src/compiler/typesharp_parser/Readme.md
+++ b/src/compiler/typesharp_parser/Readme.md
@@ -1,2 +1,2 @@
# typesharp_parser
-The typesharp parser is responsible for "parsing" or identifying tokens after `tokenization`.
\ No newline at end of file
+The typesharp parser is responsible for "parsing" or identifying tokens after `tokenization`.
diff --git a/src/compiler/typesharp_parser/mod.rs b/src/compiler/typesharp_parser/mod.rs
index b2819a7..f977b0d 100644
--- a/src/compiler/typesharp_parser/mod.rs
+++ b/src/compiler/typesharp_parser/mod.rs
@@ -1 +1,11 @@
-pub mod parser;
\ No newline at end of file
+pub mod op;
+pub mod parser;
+
+pub use self::{ op::*, parser::* };
+use crate::{ compiler::typesharp_lexer::Token };
+
+pub struct Parser;
+
+impl Parser {
+ // fn singleton_parse(stream: Vec) {}
+}
\ No newline at end of file
diff --git a/src/compiler/typesharp_parser/op.rs b/src/compiler/typesharp_parser/op.rs
new file mode 100644
index 0000000..7ffe3f8
--- /dev/null
+++ b/src/compiler/typesharp_parser/op.rs
@@ -0,0 +1,147 @@
+#[derive(Clone, PartialEq, Debug)]
+pub enum AnyOp {
+ BinOp(BinOp),
+ UnaryOp(UnaryOp),
+ LogicalOp(LogicalOp),
+ ComparisonOp(ComparisonOp),
+ AssignmentOp(AssignmentOp),
+}
+
+// Binary Operators
+#[derive(Clone, PartialEq, Debug)]
+pub enum BinOp {
+ // +
+ Plus,
+
+ // -
+ Minus,
+
+ // *
+ Star,
+
+ // /
+ Slash,
+
+ // %
+ Percent,
+
+ // ^
+ Caret,
+
+ // &
+ And,
+
+ // |
+ Or,
+
+ // <<
+ Sh1,
+
+ // >>
+ Shr,
+
+ // >>>
+ UShr,
+}
+
+#[derive(Clone, PartialEq, Debug)]
+pub enum UnaryOp {
+ // ++x
+ IncP,
+
+ // x++
+ Inc,
+
+ // --x
+ DecP,
+
+ // x--
+ Dec,
+
+ // -x
+ Neg,
+
+ // +x
+ Pos,
+
+ // !x
+ Not,
+
+ // experimental delete x
+ Delete,
+
+ // A syntax sugar for x = {}
+ Object,
+}
+
+#[derive(Clone, PartialEq, Debug)]
+pub enum LogicalOp {
+ // x && y
+ And,
+
+ // x || y
+ Or,
+
+ // x ?? y
+ Coalasce,
+}
+
+#[derive(Clone, PartialEq, Debug)]
+pub enum ComparisonOp {
+ Eq,
+
+ NotEq,
+
+ GreaterThan,
+
+ GreaterThanOrEqual,
+
+ LessThan,
+
+ LessThanOrEqual,
+
+ Contains,
+
+ In,
+
+ InstanceOf,
+}
+
+#[derive(Clone, PartialEq, Debug)]
+pub enum AssignmentOp {
+ // x += y
+ Add,
+
+ // x -= y
+ Sub,
+
+ // x *= y
+ Mul,
+
+ // x /= y
+ Div,
+
+ // x %= y
+ Rem,
+
+ And,
+
+ Or,
+
+ Xor,
+
+ Sh1,
+
+ Shr,
+
+ Ushr,
+
+ // [EXPERIMENT] x &&= y
+ BoolAnd,
+
+ // [EXPERIMENT] x ||= y
+ BoolOr,
+
+ // [EXPERIMENT] x ??= y : Support may not be in future versions
+ Coalesce,
+}
diff --git a/src/compiler/typesharp_parser/parser.rs b/src/compiler/typesharp_parser/parser.rs
index f55020e..d2e9175 100644
--- a/src/compiler/typesharp_parser/parser.rs
+++ b/src/compiler/typesharp_parser/parser.rs
@@ -1,48 +1,44 @@
-use crate::{ compiler::{
- typesharp_ast::ast,
- typesharp_lexer::Token
-}};
-
-pub struct TypeSharpRCTX {
- pub resources: Vec,
- pub scopes: Vec,
- pub current: Option
-}
-
-impl ast::IContext for TypeSharpRCTX {
- fn new(&self) -> Self {
- return Self {
- resources: vec!(),
- scopes: vec!(),
- current: None
- }
- }
-
-
- // Gets the current resource to be parsed.
- fn getCurrentResource(&self) -> Option {
- return self.current.clone();
- }
-
- /// Gets the current AST scope
- fn getCurrentScope(&self) -> Option<&ast::ASTStatement> {
- if self.scopes.len() > 0 {
- self.scopes.get(self.scopes.len())
- } else {
- None
- }
- }
-
- /// Gets all AST scopes
- fn getScopes(&self) -> Vec {
- self.scopes.clone()
- }
-
- fn nextResource(&self) -> bool {
- true
- }
-}
-
-// Typesharp parser
-pub fn parse(mut curr: &ast::AST, tokens: &Vec) {
-}
\ No newline at end of file
+use crate::compiler::{typesharp_ast::ast, typesharp_lexer::Token};
+
+// pub struct TypeSharpRCTX {
+// pub resources: Vec,
+// pub scopes: Vec,
+// pub current: Option
+// }
+
+// impl ast::IContext for TypeSharpRCTX {
+// fn new(&self) -> Self {
+// return Self {
+// resources: vec!(),
+// scopes: vec!(),
+// current: None
+// }
+// }
+
+// // Gets the current resource to be parsed.
+// fn getCurrentResource(&self) -> Option {
+// return self.current.clone();
+// }
+
+// /// Gets the current AST scope
+// fn getCurrentScope(&self) -> Option<&ast::ASTStatement> {
+// if self.scopes.len() > 0 {
+// self.scopes.get(self.scopes.len())
+// } else {
+// None
+// }
+// }
+
+// /// Gets all AST scopes
+// fn getScopes(&self) -> Vec {
+// self.scopes.clone()
+// }
+
+// fn nextResource(&self) -> bool {
+// true
+// }
+// }
+
+// // Typesharp parser
+// pub fn parse(mut curr: &ast::AST, tokens: &Vec) {
+// }
diff --git a/src/error/Readme.md b/src/error/Readme.md
index 52b06dc..a0ebb06 100644
--- a/src/error/Readme.md
+++ b/src/error/Readme.md
@@ -1,3 +1,3 @@
# Error - Typesharp Error Handling.
- - **[typesharp_error](https://github.com/TypeSharp/Typesharp/tree/master/src/error/typesharp_error)** - Typesharp's built in error handling module
\ No newline at end of file
+ - **[typesharp_error](https://github.com/TypeSharp/Typesharp/tree/master/src/error/typesharp_error)** - Typesharp's built in error handling module
\ No newline at end of file
diff --git a/src/error/mod.rs b/src/error/mod.rs
index be4fe93..3271283 100644
--- a/src/error/mod.rs
+++ b/src/error/mod.rs
@@ -1,5 +1,3 @@
pub mod typesharp_error;
-pub use self::{
- typesharp_error::*
-};
\ No newline at end of file
+pub use self::typesharp_error::*;
diff --git a/src/error/typesharp_error/Readme.md b/src/error/typesharp_error/Readme.md
index c4635fa..fc1e264 100644
--- a/src/error/typesharp_error/Readme.md
+++ b/src/error/typesharp_error/Readme.md
@@ -1,2 +1,2 @@
# typesharp_error
-Typesharp's built in error handling module
\ No newline at end of file
+Typesharp's built in error handling module
diff --git a/src/error/typesharp_error/error.rs b/src/error/typesharp_error/error.rs
new file mode 100644
index 0000000..b22b80b
--- /dev/null
+++ b/src/error/typesharp_error/error.rs
@@ -0,0 +1,84 @@
+use crate::{
+ compiler::{
+ typesharp_ast::ast,
+ typesharp_ast::Position,
+ typesharp_ast::Span
+ }
+};
+
+/// A enum that is a representation of error types.
+/// The errors represented here are errors that are related to
+/// The compilers syntax-checking.
+///
+/// Todo: When more features get added, create a different enum for features
+/// that are related to that feature.
+///
+/// An example would be: IO::ErrUnsupported, IO::
+pub enum ErrCompiler {
+ /// An error. (general error)
+ Error,
+
+ /// A fatal error (for possible memory related issues, or maybe even compiler issues that
+ /// the user wasn't supposed to reach).
+ Unreachable,
+
+ /// A warning.
+ Warning,
+
+ /// A suggestion.
+ Suggestion,
+
+ /// A note.
+ Note,
+}
+
+/// This are typesharp errors that aren't related to
+/// typesharps syntax checking.
+///
+/// Note: These are not related to IO, or any other feature in the stdlib.
+pub enum ErrFeature {
+ /// All went well, success.
+ Success,
+
+ /// Invalid handle that was trying to be accessed.
+ InvalidHandle,
+
+ /// Not enough memory allocated.
+ NotEnoughMemory,
+
+ /// Unable to Write to a certain destination.
+ WriteFault,
+
+ /// Unable to Read from certain destination.
+ ReadFault,
+}
+
+/// Trait that all compiler-level errors adhere to.
+trait ErrImpl {
+ fn to_str(&self) -> &'static str;
+}
+
+
+impl ErrImpl for ErrCompiler {
+ fn to_str(&self) -> &'static str {
+ match *self {
+ ErrCompiler::Error => "Compiler Error",
+ ErrCompiler::Unreachable => "Unreachable point accessed in the compiler, this is an internal compiler issue.",
+ ErrCompiler::Note => "Note:",
+ ErrCompiler::Suggestion => "Suggestion:",
+ ErrCompiler::Warning => "Warning:",
+ }
+ }
+}
+
+impl ErrImpl for ErrFeature {
+ fn to_str(&self) -> &'static str {
+ match *self {
+ ErrFeature::Success => "Sucess",
+ ErrFeature::InvalidHandle => "Invalid Handle being accessed",
+ ErrFeature::NotEnoughMemory => "Not enough memory available",
+ ErrFeature::WriteFault => "Unable to write to certain destination.",
+ ErrFeature::ReadFault => "Unable to read from certain desintation.",
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/error/typesharp_error/mod.rs b/src/error/typesharp_error/mod.rs
index 3de2bc4..747440d 100644
--- a/src/error/typesharp_error/mod.rs
+++ b/src/error/typesharp_error/mod.rs
@@ -1,20 +1,13 @@
-pub mod solution;
-use crate::{
- compiler::{
- typesharp_ast::ast,
- typesharp_ast::Position,
- typesharp_ast::Span
- }
-};
+use crate::compiler::{ typesharp_ast::ast, typesharp_ast::Position, typesharp_ast::Span };
-pub struct Error {
- context: ast::AnyContext,
- name: String,
- code: u64
-}
+// pub struct Error {
+// context: ast::AnyContext,
+// name: String,
+// code: u64
+// }
-impl Error {
- fn fromFile() {
- panic!("Method not implemented")
- }
-}
\ No newline at end of file
+// impl Error {
+// fn fromFile() {
+// panic!("Method not implemented")
+// }
+// }
diff --git a/src/error/typesharp_error/solution.rs b/src/error/typesharp_error/solution.rs
deleted file mode 100644
index 3ce9417..0000000
--- a/src/error/typesharp_error/solution.rs
+++ /dev/null
@@ -1,32 +0,0 @@
-use crate::{
- compiler::{
- typesharp_ast::ast,
- typesharp_ast::Position,
- typesharp_ast::Span
- }
-};
-
-pub trait PossibleSolutionGenerator {
- /// This function should check whether or not the given AST can be corrected.
- /// Keep in mind that all AST given here is during Parsing.
- fn is_corrective(&self, tree: ast::ASTStatement) -> bool;
-
- /// Should check the given AST and correct it (if acclible) and return None otherwise.
- fn try_correct(&self, tree: ast::ASTStatement) -> Option;
-
- /// Displays the correct line of code.
- fn correct_display(&self, tree: ast::ASTStatement) -> String;
-}
-
-pub enum PossibleSolutionType {
- Inline,
- Descriptive,
- Replacement
-}
-
-pub struct PossibleSolution {
- // pub resource: ast::IContextResource,
- pub loc: Span,
- pub solutions: Vec,
- pub typ: PossibleSolutionType
-}
\ No newline at end of file
diff --git a/src/lib.rs b/src/lib.rs
index 932acf8..0c2e2a4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,4 @@
-#![allow(non_snake_case)]
+#![allow(unused_imports, non_camel_case_types, dead_code)]
pub mod compiler;
pub mod error;
-//pub mod parser_h::{ parser, compile };
\ No newline at end of file
+//pub mod parser_h::{ parser, compile };
diff --git a/test/src/main.rs b/test/src/main.rs
index 307cf91..2da63dc 100644
--- a/test/src/main.rs
+++ b/test/src/main.rs
@@ -1,13 +1,9 @@
-use crate::{ typesharp::* };
-use crate::{ typesharp::compiler::typesharp_ast_old::Features };
+use typesharp;
+// use crate::{ typesharp::compiler::typesharp_ast_old::Features };
use std::fs::write;
fn main() {
- let code = "use std.math;
- /*const mychild: string = 10;
- const newval = (int) mychild;
- debug!(newval); // Number
- */";
- let lexed = compiler::typesharp_lexer::tokenize(&code);
+ let code = "\"this is a test string\\\" with a qoutation escape sequence\"\"and another string next to that\"";
+ let lexed = typesharp::compiler::typesharp_lexer::tokenize(&code);
let val = lexed.len();
for token in lexed {
@@ -16,15 +12,4 @@ fn main() {
println!("There are: {} tokens.", val);
- let bin = compile(code, compiler::typesharp_ast_two::ASTOpts {
- assembler: compiler::typesharp_ast_two::Assembler::LLVM,
- mangle: false,
- library: false,
- entry: "main",
- features: Features::Defaults,
- imports: vec!()
- debug: true
- });
-
- write("test.exe", bin.to_bytes());
}