Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions .dune-prelude
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# _
# /\ | |
# / \ __| | __ _ _ __ ___
# / /\ \ / _` |/ _` | '_ ` _ \
# / ____ \ (_| | (_| | | | | | |
# /_/ __\_\__,_|\__,_|_| |_| |_| _ _ _
# | \/ | | __ \ (_) | ( )
# | \ / | ___| | | | __ _ _ __ _ ___| |/ ___
# _
# /\ | |
# / \ __| | __ _ _ __ ___
# / /\ \ / _` |/ _` | '_ ` _ \
# / ____ \ (_| | (_| | | | | | |
# /_/ __\_\__,_|\__,_|_| |_| |_| _ _ _
# | \/ | | __ \ (_) | ( )
# | \ / | ___| | | | __ _ _ __ _ ___| |/ ___
# | |\/| |/ __| | | |/ _` | '_ \| |/ _ \ | / __|
# | | | | (__| |__| | (_| | | | | | __/ | \__ \
# |_|__|_|\___|_____/ \__,_|_|_|_|_|\___|_| |___/
# | __ \ | | | |
# | |__) | __ ___| |_ _ __| | ___
# | ___/ '__/ _ \ | | | |/ _` |/ _ \
# | | | | | __/ | |_| | (_| | __/
# |_| |_| \___|_|\__,_|\__,_|\___|
# | __ \ | | | |
# | |__) | __ ___| |_ _ __| | ___
# | ___/ '__/ _ \ | | | |/ _` |/ _ \
# | | | | | __/ | |_| | (_| | __/
# |_| |_| \___|_|\__,_|\__,_|\___|
#
# Overview:
# Splash screen:
Expand All @@ -39,7 +39,7 @@ let open = fs@read;
let concat = x -> y -> (str y) + (str x);

let ssh = addr -> user -> {
'ssh -X (str user) + "@" + (str addr);
ssh -X (str user) + "@" + (str addr);
};

let scp = addr -> user -> src -> dst -> {
Expand Down Expand Up @@ -81,15 +81,15 @@ let MOON-PHASES = [
];

let WEATHER-ASCII = {
Unknown = " .-.
__)
(
`-’
Unknown = " .-.
__)
(
`-’
•",
Sunny = " \\ /
.-.
― ( ) ―
`-’
Sunny = " \\ /
.-.
― ( ) ―
`-’
/ \\ ",
PartlyCloudy =" \\ /
_ /\"\".-.
Expand Down Expand Up @@ -177,7 +177,7 @@ let PLANTS = [" _ _
(_\\_)
(__<_{}
(_/_)
|\\ |
|\\ |
\\\\| /|
\\|//
|/
Expand Down Expand Up @@ -279,7 +279,7 @@ let get-weather-ascii = desc -> {
else WEATHER-ASCII@Unknown
};

let reload-weather-api = _ ~> {
let reload-weather-api = _ ~> {
let weather-api = units -> city -> {
# Confirm that the API key is not None
if WEATHER-API-KEY == None {
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/target
/target*
history.txt
*.cast
25 changes: 22 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
# Changelog
All notable changes to this project will be documented in this file.
## [0.2.0-symbol]

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
this branch fix symbols:

- allow nonspace operators.
such as `let a=2+3;` `a>3` `let add=x->x+1`

but space is needed when you need to differ negtive numbers with operator:
such as `let a=2+ -3`

- allow args in command.
such as `ls -l --color=auto /tmp`
+ short args: `-c`
+ long args: `--chars`
+ paths: `./dir` or `/dir` or `..`

but unfortunlately, single `/` is not added currently, as this may be used as operator someday.

single `.` was ignored and default to cwd.

- allow `:` to define dict.
`let dict={x:1,y:2}` as well as the old one :
`let dict={x=1,y=2}`

## [Unreleased]

Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dune"
version = "0.2.0-alpha"
version = "0.2.0-symbol"
authors = ["Adam McDaniel <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand Down
50 changes: 25 additions & 25 deletions src/.default-dune-prelude
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# _____ _
# | __ \ ( )
# | | | |_ _ _ __ ___|/ ___
# | | | | | | | '_ \ / _ \ / __|
# | |__| | |_| | | | | __/ \__ \
# |_____/ \__,_|_| |_|\___| |___/
# | __ \ / _| | | |
# | | | | ___| |_ __ _ _ _| | |_
# | | | |/ _ \ _/ _` | | | | | __|
# | |__| | __/ || (_| | |_| | | |_
# |_____/ \___|_| \__,_|\__,_|_|\__|
# | __ \ | | | |
# | |__) | __ ___| |_ _ __| | ___
# _____ _
# | __ \ ( )
# | | | |_ _ _ __ ___|/ ___
# | | | | | | | '_ \ / _ \ / __|
# | |__| | |_| | | | | __/ \__ \
# |_____/ \__,_|_| |_|\___| |___/
# | __ \ / _| | | |
# | | | | ___| |_ __ _ _ _| | |_
# | | | |/ _ \ _/ _` | | | | | __|
# | |__| | __/ || (_| | |_| | | |_
# |_____/ \___|_| \__,_|\__,_|_|\__|
# | __ \ | | | |
# | |__) | __ ___| |_ _ __| | ___
# | ___/ '__/ _ \ | | | |/ _` |/ _ \
# | | | | | __/ | |_| | (_| | __/
# |_| |_| \___|_|\__,_|\__,_|\___|
Expand Down Expand Up @@ -39,7 +39,7 @@ let open = fs@read;
let concat = x -> y -> (str y) + (str x);

let ssh = addr -> user -> {
'ssh -X (str user) + "@" + (str addr);
ssh -X (str user) + "@" + (str addr);
};

let scp = addr -> user -> src -> dst -> {
Expand Down Expand Up @@ -81,15 +81,15 @@ let MOON-PHASES = [
];

let WEATHER-ASCII = {
Unknown = " .-.
__)
(
`-’
Unknown = " .-.
__)
(
`-’
•",
Sunny = " \\ /
.-.
― ( ) ―
`-’
Sunny = " \\ /
.-.
― ( ) ―
`-’
/ \\ ",
PartlyCloudy =" \\ /
_ /\"\".-.
Expand Down Expand Up @@ -177,7 +177,7 @@ let PLANTS = [" _ _
(_\\_)
(__<_{}
(_/_)
|\\ |
|\\ |
\\\\| /|
\\|//
|/
Expand Down Expand Up @@ -279,7 +279,7 @@ let get-weather-ascii = desc -> {
else WEATHER-ASCII@Unknown
};

let reload-weather-api = _ ~> {
let reload-weather-api = _ ~> {
let weather-api = units -> city -> {
# Confirm that the API key is not None
if WEATHER-API-KEY == None {
Expand Down Expand Up @@ -755,4 +755,4 @@ if WEATHER-API-KEY == None {
echo (fmt@italics (fmt@red "Please set your `openweathermap.org` API key and your city with the `set-weather-api-key` macro in your .dune-prelude or .dune-secrets file!"));

echo (fmt@green "You can remove these messages by editing your .dune-prelude!")
}
}
21 changes: 13 additions & 8 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ fn syntax_highlight(line: &str) -> String {
is_colored = true;
result.push_str(k);
}
(TokenKind::StringRaw, s) => {
result.push_str("\x1b[38;5;203m");
is_colored = true;
result.push_str(s);
}
(TokenKind::StringLiteral, s) => {
result.push_str("\x1b[38;5;208m");
is_colored = true;
Expand Down Expand Up @@ -546,14 +551,14 @@ fn run_file(path: PathBuf, env: &mut Environment) -> Result<Expression, Error> {
fn main() -> Result<(), Error> {
let matches = App::new(
r#"
888
888
888
.d88888 888 888 88888b. .d88b.
d88" 888 888 888 888 "88b d8P Y8b
888 888 888 888 888 888 88888888
Y88b 888 Y88b 888 888 888 Y8b.
"Y88888 "Y88888 888 888 "Y8888
888
888
888
.d88888 888 888 88888b. .d88b.
d88" 888 888 888 888 "88b d8P Y8b
888 888 888 888 888 888 88888888
Y88b 888 Y88b 888 888 888 Y8b.
"Y88888 "Y88888 888 888 "Y8888
"#,
)
.author(crate_authors!())
Expand Down
4 changes: 2 additions & 2 deletions src/binary/init/operator_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ pub fn get(env: &mut Environment) -> Expression {
);

tmp.define(
"//",
"/",
curry(
Expression::builtin(
"//",
"/",
|args, env| {
let mut result = args[0].clone().eval(env)?;
for arg in &args[1..] {
Expand Down
10 changes: 10 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub enum Error {
ProgramNotFound(String),
SyntaxError(Str, SyntaxError),
CustomError(String),
Redeclaration(String),
UndeclaredVariable(String),
}

impl Error {
Expand Down Expand Up @@ -58,13 +60,21 @@ impl Error {
Self::PermissionDenied(..) => Self::ERROR_CODE_CUSTOM_ERROR,
Self::ProgramNotFound(..) => Self::ERROR_CODE_CUSTOM_ERROR,
Self::SyntaxError(..) => Self::ERROR_CODE_CUSTOM_ERROR,
Self::Redeclaration(..) => Self::ERROR_CODE_CUSTOM_ERROR,
Self::UndeclaredVariable(..) => Self::ERROR_CODE_CUSTOM_ERROR,
}
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Redeclaration(name) => {
write!(f, "redeclaration of {:?}", name)
}
Self::UndeclaredVariable(name) => {
write!(f, "undeclared var: {:?}", name)
}
Self::CannotApply(expr, args) => {
write!(f, "cannot apply `{:?}` to the arguments {:?}", expr, args)
}
Expand Down
33 changes: 29 additions & 4 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub enum Expression {
Map(BTreeMap<String, Self>),
None,

Del(String), // 新增删除操作
Declare(String, Box<Self>),
// Assign an expression to a variable
Assign(String, Box<Self>),

Expand Down Expand Up @@ -204,7 +206,9 @@ impl fmt::Debug for Expression {
.join("; ")
),

Self::Assign(name, expr) => write!(f, "let {} = {:?}", name, expr),
Self::Del(name) => write!(f, "del {}", name),
Self::Declare(name, expr) => write!(f, "let {} = {:?}", name, expr),
Self::Assign(name, expr) => write!(f, "{} = {:?}", name, expr),
Self::If(cond, true_expr, false_expr) => {
write!(f, "if {:?} {:?} else {:?}", cond, true_expr, false_expr)
}
Expand Down Expand Up @@ -333,7 +337,9 @@ impl fmt::Display for Expression {
.join("; ")
),

Self::Assign(name, expr) => write!(f, "let {} = {:?}", name, expr),
Self::Del(name) => write!(f, "del {}", name),
Self::Declare(name, expr) => write!(f, "let {} = {:?}", name, expr),
Self::Assign(name, expr) => write!(f, "{} = {:?}", name, expr),
Self::If(cond, true_expr, false_expr) => {
write!(f, "if {:?} {:?} else {:?}", cond, true_expr, false_expr)
}
Expand Down Expand Up @@ -423,7 +429,8 @@ impl Expression {
| Self::Bytes(_)
| Self::String(_)
| Self::Boolean(_)
| Self::Builtin(_) => vec![],
| Self::Builtin(_)
| Self::Del(_) => vec![],

Self::For(_, list, body) => {
let mut result = vec![];
Expand Down Expand Up @@ -451,6 +458,7 @@ impl Expression {
Self::Lambda(_, body, _) => body.get_used_symbols(),
Self::Macro(_, body) => body.get_used_symbols(),

Self::Declare(_, expr) => expr.get_used_symbols(),
Self::Assign(_, expr) => expr.get_used_symbols(),
Self::If(cond, t, e) => {
let mut result = vec![];
Expand Down Expand Up @@ -491,8 +499,25 @@ impl Expression {
None => Self::Symbol(name.clone()),
})
}

Self::Del(name) => {
env.undefine(&name);
return Ok(Self::None);
}
// 处理变量声明(仅允许未定义变量)
Self::Declare(name, expr) => {
// TODO: redefine is rejected. but why never report err?
if env.is_defined(&name) {
return Err(Error::Redeclaration(name));
}
let value = expr.eval_mut(env, depth + 1)?;
env.define(&name, value); // 新增 declare 方法
return Ok(Self::None);
}
Self::Assign(name, expr) => {
// TODO: enable check while in strict mode.
// if !env.is_defined(&name) {
// return Err(Error::UndeclaredVariable(name));
// }
let x = expr.eval_mut(env, depth + 1)?;
env.define(&name, x);
return Ok(Self::None);
Expand Down
Loading