Skip to content

Commit

Permalink
beta 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Spu7Nix committed Dec 26, 2020
1 parent b5b729b commit 1ae4dd9
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 25 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file added spwn-lang/.DS_Store
Binary file not shown.
26 changes: 13 additions & 13 deletions spwn-lang/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,19 @@ pub struct Variable {
pub comment: Comment,
}

// impl Variable {
// pub fn to_expression(&self) -> Expression {
// if let ValueBody::Expression(e) = &self.value.body {
// if self.path.is_empty() {
// return e.to_owned();
// }
// }
// Expression {
// values: vec![self.clone()],
// operators: Vec::new(),
// }
// }
// }
impl Variable {
pub fn to_expression(&self) -> Expression {
if let ValueBody::Expression(e) = &self.value.body {
if self.path.is_empty() {
return e.to_owned();
}
}
Expression {
values: vec![self.clone()],
operators: Vec::new(),
}
}
}

#[derive(Clone, PartialEq, Debug)]
pub struct Expression {
Expand Down
57 changes: 56 additions & 1 deletion spwn-lang/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl Value {
}
}

pub const BUILTIN_LIST: [&str; 34] = [
pub const BUILTIN_LIST: [&str; 36] = [
"print",
"sin",
"cos",
Expand All @@ -255,6 +255,8 @@ pub const BUILTIN_LIST: [&str; 34] = [
"current_context",
"append",
"matches",
"b64encrypt",
"b64decrypt",
//operators
"_or_",
"_and_",
Expand Down Expand Up @@ -315,6 +317,59 @@ pub fn built_in_function(
Value::Bool(val.matches_pat(&pattern, &info, globals, context)?)
}

"b64encrypt" => {
if arguments.len() != 1 {
return Err(RuntimeError::BuiltinError {
message: "expected one argument: string to be encrypted".to_string(),
info,
});
}

let val = globals.stored_values[arguments[0]].clone();
match val {
Value::Str(s) => {
let encrypted = base64::encode(&s.as_bytes());
Value::Str(encrypted)
}
_ => {
return Err(RuntimeError::BuiltinError {
message: "expected one argument: string to be encrypted".to_string(),
info,
})
}
}
}
"b64decrypt" => {
if arguments.len() != 1 {
return Err(RuntimeError::BuiltinError {
message: "expected one argument: string to be encrypted".to_string(),
info,
});
}

let val = globals.stored_values[arguments[0]].clone();
match val {
Value::Str(s) => {
let decrypted = match base64::decode(&s) {
Ok(s) => s,
Err(e) => {
return Err(RuntimeError::BuiltinError {
message: format!("Base 64 error: {}", e),
info,
})
}
};
Value::Str(String::from_utf8_lossy(&decrypted).to_string())
}
_ => {
return Err(RuntimeError::BuiltinError {
message: "expected one argument: string to be encrypted".to_string(),
info,
})
}
}
}

// "is_in_use" => {
// if arguments.len() != 1 {
// return Err(RuntimeError::BuiltinError {
Expand Down
6 changes: 3 additions & 3 deletions spwn-lang/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ pub fn import_module(
.expect("Your file must be in a folder to import modules!")
.join(&p),

ImportType::Lib(name) => match std::env::current_dir() {
ImportType::Lib(name) => match std::env::current_exe() {
//change to current_exe before release
Ok(p) => p,
Err(e) => {
Expand All @@ -923,8 +923,8 @@ pub fn import_module(
})
}
}
//.parent() //ADD BACK BEFORE RELEASE
//.unwrap()
.parent() //ADD BACK BEFORE RELEASE
.unwrap()
.join("libraries")
.join(name),
};
Expand Down
6 changes: 5 additions & 1 deletion spwn-lang/src/compiler_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ impl ValStorage {
}

pub fn increment_single_lifetime(&mut self, index: usize, amount: u16) {
(*self.map.get_mut(&index).expect(&(index.to_string() + " index not found"))).3 += amount;
let val = &mut (*self.map.get_mut(&index).expect(&(index.to_string() + " index not found"))).3;
if *val < 10000 - amount {
*val += amount;
}

match self[index].clone() {
Value::Array(a) => {
for e in a {
Expand Down
2 changes: 1 addition & 1 deletion spwn-lang/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let (statements, notes) = match parse_spwn(unparsed, script_path.clone()) {
Err(err) => {
eprint_with_color(&format!("{}\n", err), Color::Red);
eprint_with_color(&format!("{}\n", err), Color::White);
std::process::exit(ERROR_EXIT_CODE);
}
Ok(p) => p,
Expand Down
46 changes: 46 additions & 0 deletions spwn-lang/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::builtin::BUILTIN_LIST;
//use std::collections::HashMap;
use std::path::PathBuf;

//use ast::ValueLiteral;
use logos::Lexer;
use logos::Logos;

Expand Down Expand Up @@ -1239,7 +1240,37 @@ fn parse_dict(
let expr = parse_expr(tokens, notes, true, true)?;
defs.push(ast::DictDef::Def((symbol, expr)));
}
Some(Token::Comma) => {
if symbol == "type" {
return Err(SyntaxError::ExpectedErr {
expected: "':'".to_string(),
found: String::from("comma (',')"),
pos: tokens.position(),
file: notes.file.clone(),
});
}
defs.push(ast::DictDef::Def((
symbol.clone(),
ast::ValueBody::Symbol(symbol).to_variable().to_expression(),
)));
}

Some(Token::ClosingCurlyBracket) => {
if symbol == "type" {
return Err(SyntaxError::ExpectedErr {
expected: "':'".to_string(),
found: String::from("}"),
pos: tokens.position(),
file: notes.file.clone(),
});
}
defs.push(ast::DictDef::Def((
symbol.clone(),
ast::ValueBody::Symbol(symbol).to_variable().to_expression(),
)));
//tokens.previous();
break;
}
a => {
return Err(SyntaxError::ExpectedErr {
expected: "':'".to_string(),
Expand Down Expand Up @@ -1967,6 +1998,21 @@ fn parse_variable(
tokens.previous();
ast::ValueBody::Dictionary(parse_dict(tokens, notes)?)
}
Some(Token::Symbol) => match tokens.next(false, false) {
Some(Token::ClosingCurlyBracket) | Some(Token::Comma) | Some(Token::Colon) => {
tokens.previous();
tokens.previous();
ast::ValueBody::Dictionary(parse_dict(tokens, notes)?)
}
_ => {
tokens.previous();
tokens.previous();

ast::ValueBody::CmpStmt(ast::CompoundStatement {
statements: parse_cmp_stmt(tokens, notes)?,
})
}
},
_ => match tokens.next(false, false) {
Some(Token::Colon) => {
tokens.previous();
Expand Down
9 changes: 3 additions & 6 deletions spwn-lang/test/test.spwn
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ impl @binaryCalculator {



//extract obj_props;
//import "binaryCalculator.spwn";

decimal = ?i;
decimalCounter = counter(decimal);
calculator = @binaryCalculator::new(8, 300, 300);
calculator = @binaryCalculator::new(16, 300, 300);

$.add(obj {
OBJ_ID: 1615,
Expand All @@ -78,6 +75,6 @@ $.add(obj {
GROUPS: 999g
});

decimalCounter = 3576;
decimalCounter = 64576;
wait(1);
calculator.decimalToBinary(decimalCounter, calcSpeed = 8);
calculator.prettyDecimalToBinary(decimalCounter);

0 comments on commit 1ae4dd9

Please sign in to comment.