Skip to content

Commit

Permalink
feat(vectors, ifelse): add them
Browse files Browse the repository at this point in the history
  • Loading branch information
megatank58 committed May 6, 2023
1 parent cfadc8b commit 656c948
Show file tree
Hide file tree
Showing 11 changed files with 722 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "oxido"
version = "2.3.0"
version = "2.4.0"
edition = "2021"

[profile.release]
Expand Down
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Oxido

**Table of Contents**:

- [Oxido](#oxido)
- [Files](#Files)
- [Files:](#files)
- [Installation](#installation)
- [Uninstallation](#uninstallation)
- [Usage](#usage)
Expand All @@ -15,6 +13,15 @@
- [Loop statements](#loop-statements)
- [Functions](#functions)
- [Exiting](#exiting)
- [Standard Library](#standard-library)
- [IO](#io)
- [print()](#print)
- [println()](#println)
- [Types](#types)
- [str()](#str)
- [int()](#int)
- [bool()](#bool)


Oxido is a dynamic interpreted programming language basing most of its syntax on Rust.

Expand Down Expand Up @@ -65,17 +72,19 @@ Conventionally, Oxido files are named `main.oxi`.
* Int: Integers (no fractions), passing the regex `[0-9]+`.

* Bool: `true` or `false`

* Vec: A uniform collection of the other data types, denoted by `[T]`.

### Variables

Variables are declared by the `let` keyword, followed by the identifier, which must pass the regex `[A-Za-z]+`, followed by the data type and an equal sign and the expression.
Variables are declared by the `let` keyword, followed by the identifier, which must pass the regex `[A-Za-z]+`, followed by the data type (optional) and an equal sign and the expression.

For example:

```rs
let a: str = "Hi mom!";
let a: int = 5;
let a: int = 5 * 5;
let n = 5;
let z = 5 * 5;
let f: int = factorial(5);
```

Expand Down Expand Up @@ -127,8 +136,9 @@ Funcitons store the given conditions until they are called. They are declared wi
```rs
let text = "Hi mom!";

fn message(x: str) {
fn message(x: str) -> int {
print(x);
return 0;
}

message(text);
Expand Down
41 changes: 41 additions & 0 deletions examples/search.oxi
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
let a = [0];

print("Enter number of values to be read: ");

let n = int(read());
let i = 0;

loop {
if i == n {
break;
}
print("Enter value: ");
a[i] = int(read());
i = i + 1;
}

print("Enter value to be searched: ");

let x = int(read());

let found = false;

i = 0;

loop {
if i == n {
break;
}
if a[i] == x {
found = true;
break;
}
i = i + 1;
}

if found == true {
print("Value found at index: ");
print(i);
} else {
print("Value not found");
}
6 changes: 5 additions & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ pub type Ast = Vec<(AstNode, Range<usize>)>;
pub enum AstNode {
Assignment(String, Option<DataType>, Expression),
ReAssignment(String, Expression),
VecReAssignment(String, Expression, Expression),
If(Expression, Ast),
IfElse(Expression, Ast, Ast),
Loop(Ast),
FunctionCall(String, Vec<Expression>),
FunctionDeclaration(String, Vec<Param>, Option<DataType>, Ast),
Expand All @@ -23,5 +25,7 @@ pub enum Expression {
Int(i64),
Bool(bool),
FunctionCall(String, Vec<Expression>),
Identifier(String)
Identifier(String),
Vector(Vec<Expression>),
VecIndex(String, Box<Expression>),
}
12 changes: 11 additions & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub enum Data {
Str(String),
Int(i64),
Bool(bool),
Vector(Vec<Data>, DataType),
}

impl Data {
Expand All @@ -15,6 +16,7 @@ impl Data {
Data::Str(_) => "str",
Data::Int(_) => "int",
Data::Bool(_) => "bool",
Data::Vector(_, _) => "vector",
}
}

Expand All @@ -23,12 +25,14 @@ impl Data {
Data::Str(_) => DataType::Str,
Data::Int(_) => DataType::Int,
Data::Bool(_) => DataType::Bool,
Data::Vector(_, _) => DataType::Vector,
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum DataType {
Vector,
Str,
Int,
Bool,
Expand All @@ -43,6 +47,7 @@ impl Display for DataType {
DataType::Str => "str",
DataType::Int => "int",
DataType::Bool => "bool",
DataType::Vector => "vector",
})
)
}
Expand Down Expand Up @@ -81,7 +86,12 @@ pub struct Function {
}

impl Function {
pub fn new(name: String, params: Vec<Param>, datatype: Option<DataType>, statements: Ast) -> Self {
pub fn new(
name: String,
params: Vec<Param>,
datatype: Option<DataType>,
statements: Ast,
) -> Self {
Self {
name,
params,
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};

pub fn error(
name: &str,
source: &str,
file: &str,
code: &str,
message: &str,
note: &str,
range: &Range<usize>,
) -> ! {
let mut files = SimpleFiles::new();

let file_id = files.add(name, source);
let file_id = files.add(name, file);

let diagnostic = Diagnostic::error()
.with_message(message)
Expand Down
Loading

0 comments on commit 656c948

Please sign in to comment.