Skip to content

Commit 4d4db1e

Browse files
authored
Merge pull request #56 from Ph0enixKM/A85
A85 Fix bad erroring behaviour with if chain
2 parents d69aae6 + af8adee commit 4d4db1e

File tree

9 files changed

+72
-69
lines changed

9 files changed

+72
-69
lines changed

setup/install.ab

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { hasFailed } from "std"
2+
13
let name = "AmberNative"
24
let target = "amber"
3-
let tag = "0.3.0-alpha"
5+
let tag = "0.3.1-alpha"
46
let place = "/opt/amber"
57

68
unsafe {
@@ -29,34 +31,26 @@ unsafe {
2931
}
3032

3133
echo "Installing Amber"
32-
let isDone = false
33-
34-
silent $ruby -v$
35-
if status == 0 {
36-
let code = "require \"open-uri\"; open(\"{target}\", \"wb\") do |file|; file << open(\"{url}\").read; end"
37-
echo "Using ruby as a download method..."
38-
$sudo ruby -e "{code}"$
39-
isDone = true
40-
}
41-
42-
silent $curl -v$
43-
if status == 0 {
44-
echo "Using curl as a download method..."
45-
$curl -o "{target}" "{url}"$
46-
isDone = true
47-
}
48-
49-
silent $wget -v$
50-
if status == 0 {
51-
echo "Using wget as a download method..."
52-
$wget -O "{target}" "{url}"$
53-
isDone = true
54-
}
5534

56-
if not isDone {
57-
echo "Neither ruby, curl or wget are installed on your system."
58-
echo "Please install one of them and try again."
59-
$exit 1$
35+
if {
36+
not hasFailed("ruby -v") {
37+
let code = "require \"open-uri\"; open(\"{target}\", \"wb\") do |file|; file << open(\"{url}\").read; end"
38+
echo "Using ruby as a download method..."
39+
$sudo ruby -e "{code}"$
40+
}
41+
not hasFailed("curl -v") {
42+
echo "Using curl as a download method..."
43+
$curl -o "{target}" "{url}"$
44+
}
45+
not hasFailed("wget -V") {
46+
echo "Using wget as a download method..."
47+
$wget -O "{target}" "{url}"$
48+
}
49+
else {
50+
echo "Neither ruby, curl or wget are installed on your system."
51+
echo "Please install one of them and try again."
52+
$exit 1$
53+
}
6054
}
6155

6256
// Create directory for amber
@@ -69,4 +63,4 @@ unsafe {
6963
$sudo ln -s "{place}/{target}" "/usr/local/bin/{target}"$
7064
// Send success message
7165
echo "Amber has been installed successfully. 🎉"
72-
}
66+
}

setup/install.sh

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
function hasFailed__18_v0 {
2+
local command=$1
3+
${command} > /dev/null 2>&1
4+
__AMBER_STATUS=$?;
5+
if [ $__AMBER_STATUS != 0 ]; then
6+
$(exit $__AMBER_STATUS)
7+
:
8+
fi;
9+
__AMBER_FUN_hasFailed18_v0=$(echo $__AMBER_STATUS '!=' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//');
10+
return 0
11+
}
112
__0_name="AmberNative";
213
__1_target="amber";
3-
__2_tag="0.3.0-alpha";
14+
__2_tag="0.3.1-alpha";
415
__3_place="/opt/amber";
516
__AMBER_VAL_0=$(uname -s);
617
__AMBER_STATUS=$?;
@@ -36,57 +47,35 @@ $(exit $__AMBER_STATUS)
3647
fi
3748
fi;
3849
echo "Installing Amber";
39-
isDone=0;
40-
ruby -v > /dev/null 2>&1
41-
__AMBER_STATUS=$?;
42-
if [ $__AMBER_STATUS != 0 ]; then
43-
$(exit $__AMBER_STATUS)
44-
:
45-
fi;
46-
if [ $(echo $__AMBER_STATUS '==' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
50+
hasFailed__18_v0 "ruby -v";
51+
hasFailed__18_v0 "curl -v";
52+
hasFailed__18_v0 "wget -V";
53+
if [ $(echo '!' ${__AMBER_FUN_hasFailed18_v0} | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
4754
code="require \"open-uri\"; open(\"${__1_target}\", \"wb\") do |file|; file << open(\"${url}\").read; end";
4855
echo "Using ruby as a download method...";
49-
ruby -e "${code}"
50-
__AMBER_STATUS=$?;
51-
if [ $__AMBER_STATUS != 0 ]; then
52-
$(exit $__AMBER_STATUS)
53-
:
54-
fi;
55-
isDone=1
56-
fi;
57-
curl -v > /dev/null 2>&1
56+
sudo ruby -e "${code}"
5857
__AMBER_STATUS=$?;
5958
if [ $__AMBER_STATUS != 0 ]; then
6059
$(exit $__AMBER_STATUS)
6160
:
62-
fi;
63-
if [ $(echo $__AMBER_STATUS '==' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
61+
fi
62+
elif [ $(echo '!' ${__AMBER_FUN_hasFailed18_v0} | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
6463
echo "Using curl as a download method...";
6564
curl -o "${__1_target}" "${url}"
6665
__AMBER_STATUS=$?;
6766
if [ $__AMBER_STATUS != 0 ]; then
6867
$(exit $__AMBER_STATUS)
6968
:
70-
fi;
71-
isDone=1
72-
fi;
73-
wget -v > /dev/null 2>&1
74-
__AMBER_STATUS=$?;
75-
if [ $__AMBER_STATUS != 0 ]; then
76-
$(exit $__AMBER_STATUS)
77-
:
78-
fi;
79-
if [ $(echo $__AMBER_STATUS '==' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
69+
fi
70+
elif [ $(echo '!' ${__AMBER_FUN_hasFailed18_v0} | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
8071
echo "Using wget as a download method...";
8172
wget -O "${__1_target}" "${url}"
8273
__AMBER_STATUS=$?;
8374
if [ $__AMBER_STATUS != 0 ]; then
8475
$(exit $__AMBER_STATUS)
8576
:
86-
fi;
87-
isDone=1
88-
fi;
89-
if [ $(echo '!' ${isDone} | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
77+
fi
78+
else
9079
echo "Neither ruby, curl or wget are installed on your system.";
9180
echo "Please install one of them and try again.";
9281
exit 1

src/modules/condition/ifchain.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ impl SyntaxModule<ParserMetadata> for IfChain {
3838
is_else = true;
3939
break
4040
}
41-
// Handle end of the if chain
42-
if syntax(meta, &mut cond).is_err() {
41+
if token(meta, "}").is_ok() {
4342
break
4443
}
44+
// Handle end of the if chain
45+
if let Err(err) = syntax(meta, &mut cond) {
46+
return Err(err)
47+
}
4548
match token(meta, "{") {
4649
Ok(_) => {
4750
syntax(meta, &mut block)?;

src/modules/expression/binop/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use heraclitus_compiler::prelude::*;
2-
use crate::{utils::{metadata::ParserMetadata}, modules::types::{Type, Typed}};
2+
use crate::{utils::metadata::ParserMetadata, modules::types::{Type, Typed}};
33
use super::super::expression::expr::Expr;
44

55
pub mod add;

src/modules/function/declaration.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ impl SyntaxModule<ParserMetadata> for FunctionDeclaration {
110110
self.arg_types.push(Type::Generic);
111111
}
112112
}
113+
let tok = meta.get_current_token();
114+
if token(meta, "=").is_ok() {
115+
return error!(meta, tok, "Default values for function arguments are not yet supported")
116+
}
113117
match token(meta, ")") {
114118
Ok(_) => break,
115119
Err(_) => token(meta, ",")?

src/modules/function/invocation_utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ fn run_function_with_args(meta: &mut ParserMetadata, mut fun: FunctionDecl, args
4646
}
4747
let mut ctx = meta.fun_cache.get_context(fun.id).unwrap().clone();
4848
let mut block = Block::new();
49+
let mut binop_border = None;
4950
// Swap the contexts to use the function context
5051
swap(&mut ctx, &mut meta.context);
52+
// Swap the binop border to clear it
53+
swap(&mut binop_border, &mut meta.binop_border);
5154
// Create a sub context for new variables
5255
meta.push_scope();
5356
for (kind, name, is_ref) in izip!(args, &fun.arg_names, &fun.arg_refs) {
@@ -63,6 +66,8 @@ fn run_function_with_args(meta: &mut ParserMetadata, mut fun: FunctionDecl, args
6366
meta.pop_scope();
6467
// Restore old context
6568
swap(&mut ctx, &mut meta.context);
69+
// Restore old binop border
70+
swap(&mut binop_border, &mut meta.binop_border);
6671
// Set the new return type or null if nothing was returned
6772
if let Type::Generic = fun.returns {
6873
fun.returns = ctx.fun_ret_type.clone().unwrap_or_else(|| Type::Null);

src/modules/statement/stmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl TranslateModule for Statement {
151151
// Translate the statement
152152
let translated = self.translate_match(meta, self.value.as_ref().unwrap());
153153
// This is a workaround that handles $(...) which cannot be used as a statement
154-
let translated = (matches!(self.value, Some(StatementType::Expr(_))) || translated.starts_with('$') || translated.starts_with("\"$"))
154+
let translated = (matches!(self.value, Some(StatementType::Expr(_))) || translated.starts_with("$(") || translated.starts_with("\"$("))
155155
.then(|| format!("echo {} > /dev/null 2>&1", translated))
156156
.unwrap_or_else(|| translated);
157157
// Get all the required supplemental statements

src/std/main.ab

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ pub fun parse(text: Text): Num {
7373
return text as Num
7474
}
7575

76-
#[allow_absurd_cast]
7776
pub fun chars(text: Text): [Text] {
7877
let chars = [Text]
7978
unsafe $for ((i=0; i<\$\{#{nameof text}}; i++)); do
@@ -86,3 +85,12 @@ pub fun chars(text: Text): [Text] {
8685
pub fun sum(list: [Num]): Num {
8786
return unsafe $echo "{list}" | awk '\{s=0; for (i=1; i<=NF; i++) s+=\$i; print s}'$ as Num
8887
}
88+
89+
pub fun hasFailed(command: Text): Bool {
90+
unsafe silent ${command}$
91+
return status != 0
92+
}
93+
94+
pub fun exit(code: Num): Null {
95+
unsafe $exit "{code}"$
96+
}

src/tests/validity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ fn status() {
725725
echo status
726726
}
727727
";
728-
test_amber!(code, "127\n0");
728+
test_amber!(code, "127\n127");
729729
}
730730

731731
#[test]

0 commit comments

Comments
 (0)