Skip to content

Commit

Permalink
Merge pull request #56 from Ph0enixKM/A85
Browse files Browse the repository at this point in the history
A85 Fix bad erroring behaviour with if chain
  • Loading branch information
Ph0enixKM authored Sep 7, 2023
2 parents d69aae6 + af8adee commit 4d4db1e
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 69 deletions.
52 changes: 23 additions & 29 deletions setup/install.ab
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { hasFailed } from "std"

let name = "AmberNative"
let target = "amber"
let tag = "0.3.0-alpha"
let tag = "0.3.1-alpha"
let place = "/opt/amber"

unsafe {
Expand Down Expand Up @@ -29,34 +31,26 @@ unsafe {
}

echo "Installing Amber"
let isDone = false

silent $ruby -v$
if status == 0 {
let code = "require \"open-uri\"; open(\"{target}\", \"wb\") do |file|; file << open(\"{url}\").read; end"
echo "Using ruby as a download method..."
$sudo ruby -e "{code}"$
isDone = true
}

silent $curl -v$
if status == 0 {
echo "Using curl as a download method..."
$curl -o "{target}" "{url}"$
isDone = true
}

silent $wget -v$
if status == 0 {
echo "Using wget as a download method..."
$wget -O "{target}" "{url}"$
isDone = true
}

if not isDone {
echo "Neither ruby, curl or wget are installed on your system."
echo "Please install one of them and try again."
$exit 1$
if {
not hasFailed("ruby -v") {
let code = "require \"open-uri\"; open(\"{target}\", \"wb\") do |file|; file << open(\"{url}\").read; end"
echo "Using ruby as a download method..."
$sudo ruby -e "{code}"$
}
not hasFailed("curl -v") {
echo "Using curl as a download method..."
$curl -o "{target}" "{url}"$
}
not hasFailed("wget -V") {
echo "Using wget as a download method..."
$wget -O "{target}" "{url}"$
}
else {
echo "Neither ruby, curl or wget are installed on your system."
echo "Please install one of them and try again."
$exit 1$
}
}

// Create directory for amber
Expand All @@ -69,4 +63,4 @@ unsafe {
$sudo ln -s "{place}/{target}" "/usr/local/bin/{target}"$
// Send success message
echo "Amber has been installed successfully. 🎉"
}
}
57 changes: 23 additions & 34 deletions setup/install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
function hasFailed__18_v0 {
local command=$1
${command} > /dev/null 2>&1
__AMBER_STATUS=$?;
if [ $__AMBER_STATUS != 0 ]; then
$(exit $__AMBER_STATUS)
:
fi;
__AMBER_FUN_hasFailed18_v0=$(echo $__AMBER_STATUS '!=' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//');
return 0
}
__0_name="AmberNative";
__1_target="amber";
__2_tag="0.3.0-alpha";
__2_tag="0.3.1-alpha";
__3_place="/opt/amber";
__AMBER_VAL_0=$(uname -s);
__AMBER_STATUS=$?;
Expand Down Expand Up @@ -36,57 +47,35 @@ $(exit $__AMBER_STATUS)
fi
fi;
echo "Installing Amber";
isDone=0;
ruby -v > /dev/null 2>&1
__AMBER_STATUS=$?;
if [ $__AMBER_STATUS != 0 ]; then
$(exit $__AMBER_STATUS)
:
fi;
if [ $(echo $__AMBER_STATUS '==' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
hasFailed__18_v0 "ruby -v";
hasFailed__18_v0 "curl -v";
hasFailed__18_v0 "wget -V";
if [ $(echo '!' ${__AMBER_FUN_hasFailed18_v0} | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
code="require \"open-uri\"; open(\"${__1_target}\", \"wb\") do |file|; file << open(\"${url}\").read; end";
echo "Using ruby as a download method...";
ruby -e "${code}"
__AMBER_STATUS=$?;
if [ $__AMBER_STATUS != 0 ]; then
$(exit $__AMBER_STATUS)
:
fi;
isDone=1
fi;
curl -v > /dev/null 2>&1
sudo ruby -e "${code}"
__AMBER_STATUS=$?;
if [ $__AMBER_STATUS != 0 ]; then
$(exit $__AMBER_STATUS)
:
fi;
if [ $(echo $__AMBER_STATUS '==' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
fi
elif [ $(echo '!' ${__AMBER_FUN_hasFailed18_v0} | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
echo "Using curl as a download method...";
curl -o "${__1_target}" "${url}"
__AMBER_STATUS=$?;
if [ $__AMBER_STATUS != 0 ]; then
$(exit $__AMBER_STATUS)
:
fi;
isDone=1
fi;
wget -v > /dev/null 2>&1
__AMBER_STATUS=$?;
if [ $__AMBER_STATUS != 0 ]; then
$(exit $__AMBER_STATUS)
:
fi;
if [ $(echo $__AMBER_STATUS '==' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
fi
elif [ $(echo '!' ${__AMBER_FUN_hasFailed18_v0} | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
echo "Using wget as a download method...";
wget -O "${__1_target}" "${url}"
__AMBER_STATUS=$?;
if [ $__AMBER_STATUS != 0 ]; then
$(exit $__AMBER_STATUS)
:
fi;
isDone=1
fi;
if [ $(echo '!' ${isDone} | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
fi
else
echo "Neither ruby, curl or wget are installed on your system.";
echo "Please install one of them and try again.";
exit 1
Expand Down
7 changes: 5 additions & 2 deletions src/modules/condition/ifchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ impl SyntaxModule<ParserMetadata> for IfChain {
is_else = true;
break
}
// Handle end of the if chain
if syntax(meta, &mut cond).is_err() {
if token(meta, "}").is_ok() {
break
}
// Handle end of the if chain
if let Err(err) = syntax(meta, &mut cond) {
return Err(err)
}
match token(meta, "{") {
Ok(_) => {
syntax(meta, &mut block)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use heraclitus_compiler::prelude::*;
use crate::{utils::{metadata::ParserMetadata}, modules::types::{Type, Typed}};
use crate::{utils::metadata::ParserMetadata, modules::types::{Type, Typed}};
use super::super::expression::expr::Expr;

pub mod add;
Expand Down
4 changes: 4 additions & 0 deletions src/modules/function/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ impl SyntaxModule<ParserMetadata> for FunctionDeclaration {
self.arg_types.push(Type::Generic);
}
}
let tok = meta.get_current_token();
if token(meta, "=").is_ok() {
return error!(meta, tok, "Default values for function arguments are not yet supported")
}
match token(meta, ")") {
Ok(_) => break,
Err(_) => token(meta, ",")?
Expand Down
5 changes: 5 additions & 0 deletions src/modules/function/invocation_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ fn run_function_with_args(meta: &mut ParserMetadata, mut fun: FunctionDecl, args
}
let mut ctx = meta.fun_cache.get_context(fun.id).unwrap().clone();
let mut block = Block::new();
let mut binop_border = None;
// Swap the contexts to use the function context
swap(&mut ctx, &mut meta.context);
// Swap the binop border to clear it
swap(&mut binop_border, &mut meta.binop_border);
// Create a sub context for new variables
meta.push_scope();
for (kind, name, is_ref) in izip!(args, &fun.arg_names, &fun.arg_refs) {
Expand All @@ -63,6 +66,8 @@ fn run_function_with_args(meta: &mut ParserMetadata, mut fun: FunctionDecl, args
meta.pop_scope();
// Restore old context
swap(&mut ctx, &mut meta.context);
// Restore old binop border
swap(&mut binop_border, &mut meta.binop_border);
// Set the new return type or null if nothing was returned
if let Type::Generic = fun.returns {
fun.returns = ctx.fun_ret_type.clone().unwrap_or_else(|| Type::Null);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/statement/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl TranslateModule for Statement {
// Translate the statement
let translated = self.translate_match(meta, self.value.as_ref().unwrap());
// This is a workaround that handles $(...) which cannot be used as a statement
let translated = (matches!(self.value, Some(StatementType::Expr(_))) || translated.starts_with('$') || translated.starts_with("\"$"))
let translated = (matches!(self.value, Some(StatementType::Expr(_))) || translated.starts_with("$(") || translated.starts_with("\"$("))
.then(|| format!("echo {} > /dev/null 2>&1", translated))
.unwrap_or_else(|| translated);
// Get all the required supplemental statements
Expand Down
10 changes: 9 additions & 1 deletion src/std/main.ab
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ pub fun parse(text: Text): Num {
return text as Num
}

#[allow_absurd_cast]
pub fun chars(text: Text): [Text] {
let chars = [Text]
unsafe $for ((i=0; i<\$\{#{nameof text}}; i++)); do
Expand All @@ -86,3 +85,12 @@ pub fun chars(text: Text): [Text] {
pub fun sum(list: [Num]): Num {
return unsafe $echo "{list}" | awk '\{s=0; for (i=1; i<=NF; i++) s+=\$i; print s}'$ as Num
}

pub fun hasFailed(command: Text): Bool {
unsafe silent ${command}$
return status != 0
}

pub fun exit(code: Num): Null {
unsafe $exit "{code}"$
}
2 changes: 1 addition & 1 deletion src/tests/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ fn status() {
echo status
}
";
test_amber!(code, "127\n0");
test_amber!(code, "127\n127");
}

#[test]
Expand Down

0 comments on commit 4d4db1e

Please sign in to comment.