Skip to content

Commit

Permalink
feat: types
Browse files Browse the repository at this point in the history
  • Loading branch information
megatank58 committed May 4, 2023
1 parent 671d269 commit 5c91bf0
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 310 deletions.
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "oxido"
version = "2.1.2"
version = "2.2.2"
edition = "2021"

[profile.release]
Expand All @@ -13,7 +13,4 @@ debug = true

[dependencies]
clap = { version = "3.1.18", features = ["derive"] }
codespan-reporting = "0.11.1"
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = [
"llvm14-0",
] }
codespan-reporting = "0.11.1"
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Conventionally, Oxido files are named `main.oxi`.

* String: A string is any value inside `"` (double quotes) passing the regex `\"[A-Za-z0-9 !]+\"`.

* Integer: Integers (no fractions), passing the regex `[0-9]+`.
* Int: Integers (no fractions), passing the regex `[0-9]+`.

* Bool: `true` or `false`

Expand All @@ -88,7 +88,7 @@ let a: int = 0;
a = 5;
a = 5 * 5;
a = factorial(5);
a = "Hi mom!"; // errors
a = "Hi mom!"; // error: incorrect data type
```

### If statements
Expand Down Expand Up @@ -139,9 +139,9 @@ message(text);
The `exit` keyword can be used to exit the program with the specified exit code

```rs
print("Hi mom!")
print("Hi mom!");

exit
exit(0);
```

## Standard Library
Expand Down
18 changes: 10 additions & 8 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
use std::ops::Range;
use crate::token::Token;
use crate::{token::Token, data::{DataType, Param}};

pub type Ast = Vec<(AstNode, Range<usize>)>;

#[derive(Clone, Debug)]
pub enum AstNode {
Assignment(String, Expression),
ReAssignment(String, Expression),
If(Expression, Vec<(AstNode, Range<usize>)>),
Loop(Vec<(AstNode, Range<usize>)>),
Assignment(String, DataType, Expression),
ReAssignment(String, DataType, Expression),
If(Expression, Ast),
Loop(Ast),
FunctionCall(String, Vec<Expression>),
FunctionDeclaration(String, Vec<String>, Vec<(AstNode, Range<usize>)>),
FunctionDeclaration(String, Vec<Param>, Ast),
Break,
Return(Expression),
Exit
Exit(Expression),
}

#[derive(Clone, Debug)]
pub enum Expression {
BinaryOperation(Box<Expression>, Token, Box<Expression>),
Str(String),
Integer(i64),
Int(i64),
Bool(bool),
FunctionCall(String, Vec<Expression>),
Identifier(String)
Expand Down
90 changes: 90 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::fmt::{self, Display};

use crate::ast::Ast;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Data {
Str(String),
Int(i64),
Bool(bool),
}

impl Data {
pub fn to_string(&self) -> &str {
match self {
Data::Str(_) => "str",
Data::Int(_) => "int",
Data::Bool(_) => "bool",
}
}

pub fn r#type(&self) -> DataType {
match self {
Data::Str(_) => DataType::Str,
Data::Int(_) => DataType::Int,
Data::Bool(_) => DataType::Bool,
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum DataType {
Str,
Int,
Bool,
}

impl Display for DataType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
String::from(match self {
DataType::Str => "str",
DataType::Int => "int",
DataType::Bool => "bool",
})
)
}
}

#[derive(Clone, Debug)]
pub struct Variable {
pub datatype: DataType,
pub data: Data,
}

impl Variable {
pub fn new(datatype: DataType, data: Data) -> Self {
Self { datatype, data }
}
}

#[derive(Clone, Debug)]
pub struct Param {
pub name: String,
pub datatype: DataType,
}

impl Param {
pub fn new(name: String, datatype: DataType) -> Self {
Self { name, datatype }
}
}

#[derive(Clone, Debug)]
pub struct Function {
pub name: String,
pub params: Vec<Param>,
pub statements: Ast,
}

impl Function {
pub fn new(name: String, params: Vec<Param>, statements: Ast) -> Self {
Self {
name,
params,
statements,
}
}
}
37 changes: 0 additions & 37 deletions src/datatype.rs

This file was deleted.

Loading

0 comments on commit 5c91bf0

Please sign in to comment.