Skip to content

Commit

Permalink
Methods, types, fixes, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
niieani committed Jan 21, 2016
1 parent 3d4ed83 commit 3add1bd
Show file tree
Hide file tree
Showing 20 changed files with 205 additions and 98 deletions.
6 changes: 5 additions & 1 deletion .idea/codeStyleSettings.xml

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

2 changes: 1 addition & 1 deletion import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ System::Import() {
DEBUG subject=level3 Log "Trying to load from: ${libPath}"
[ ! -e "$libPath" ] && throw "Cannot import $libPath" && return 1

libPath="$(File.GetAbsolutePath "$libPath")"
libPath="$(File::GetAbsolutePath "$libPath")"

if [ -d "$libPath" ]; then
local file
Expand Down
4 changes: 2 additions & 2 deletions lib/oo-framework.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ System::Bootstrap(){
local path
for file in "$__oo__libPath"/system/*.sh
do
path="$(File.GetAbsolutePath "$file")"
path="$(File::GetAbsolutePath "$file")"
__oo__importedFiles+=( "$path" )

## note: aliases are visible inside functions only if
Expand All @@ -21,7 +21,7 @@ System::Bootstrap(){
done
}

File.GetAbsolutePath() {
File::GetAbsolutePath() {
# http://stackoverflow.com/questions/3915040/bash-fish-command-to-print-absolute-path-to-a-file
# $1 : relative filename
if [[ "$file" == "/"* ]]
Expand Down
20 changes: 14 additions & 6 deletions lib/system/02_named_parameters.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Variable::TrapAssignNumberedParameter() {

# Log "TRAP: ${commandWithArgs[@]}"

if [[ "$command" == "trap" || "$command" == "l="* || "$command" == "_type="* ]]
if [[ "$command" == "trap" || "$command" == "l="* || "$command" == "_type="* || "$command" == "__capture_valueRequired="* || "$command" == "__capture_valueReadOnly="* ]]
then
return 0
fi
Expand Down Expand Up @@ -49,6 +49,14 @@ Variable::TrapAssignNumberedParameter() {
then
DEBUG subject="parameters-setting" Log "SETTING: $__assign_varName = \$$__assign_paramNo"
# subject="parameters-setting" Log --

if [[ "$__assign_valueRequired" == 'true' && -z "${!__assign_paramNo}" ]]
then
e="Value is required for the parameter [$__assign_varType] $__assign_varName ($__assign_paramNo)" throw
return
fi

unset __assign_valueRequired __assign_valueReadOnly

case "$__assign_varType" in
'params')
Expand Down Expand Up @@ -126,10 +134,6 @@ Variable::TrapAssignNumberedParameter() {
then
Type::CreateHandlerFunction "$__assign_varName" 2> /dev/null || true
fi
# if Function::Exists 'Type::CreateHandlerFunction' && [[ ! -z ${__oo__bootstrapped+x} ]]
# then
# Type::CreateHandlerFunction "$__assign_varName"
# fi
fi

if [[ "$command" != "local" || "$__assign_next" != "true" ]]
Expand All @@ -147,6 +151,8 @@ Variable::TrapAssignNumberedParameter() {
__assign_varValue="$varValue"
__assign_varType="$__capture_type"
__assign_arrLength="$__capture_arrLength"
__assign_valueRequired="$__capture_valueRequired"
__assign_valueReadOnly="$__capture_valueReadOnly"

DEBUG subject="parameters-pass" Log "PASS ${commandWithArgs[*]}"
# subject="parameters-pass" Log --
Expand All @@ -164,7 +170,7 @@ Variable::InTrapCaptureParameters() {
}

# NOTE: true; true; at the end is required to workaround an edge case where TRAP doesn't behave properly
alias Variable::TrapAssign='Variable::InTrapCaptureParameters; local -i __assign_normalCodeStarted=0; trap "declare -i __assign_paramNo; Variable::TrapAssignNumberedParameter \"\$BASH_COMMAND\" \"\$@\"; [[ \$__assign_normalCodeStarted -ge 2 ]] && trap - DEBUG && unset __assign_varType __assign_varName __assign_varValue __assign_paramNo" DEBUG; true; true; '
alias Variable::TrapAssign='Variable::InTrapCaptureParameters; local -i __assign_normalCodeStarted=0; trap "declare -i __assign_paramNo; Variable::TrapAssignNumberedParameter \"\$BASH_COMMAND\" \"\$@\"; [[ \$__assign_normalCodeStarted -ge 2 ]] && trap - DEBUG && unset __assign_varType __assign_varName __assign_varValue __assign_paramNo __assign_valueRequired __assign_valueReadOnly" DEBUG; true; true; '
alias Variable::TrapAssignLocal='Variable::TrapAssign local'
alias [reference]='_type=reference Variable::TrapAssign local -n'
alias [string]="_type=string Variable::TrapAssign local \${__assign_isReference}"
Expand All @@ -185,5 +191,7 @@ alias [string[8]]='l=8 _type=params Variable::TrapAssignLocal'
alias [string[9]]='l=9 _type=params Variable::TrapAssignLocal'
alias [string[10]]='l=10 _type=params Variable::TrapAssignLocal'
alias [...rest]='_type=rest Variable::TrapAssignLocal'
alias @required='__capture_valueRequired=true '
# TODO: alias @readonly='__capture_valueReadOnly=true '

declare -g ref=$'\UEFF1A'$'\UEFF1A'
10 changes: 5 additions & 5 deletions lib/system/05_stub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ String::GenerateSpaces() {
then
( printf "%*s" "$howMany" )
fi
return 0
}

String::ReplaceSlashes() {
Expand All @@ -45,12 +44,13 @@ String::RestoreSlashes() {
echo "${stringToMark/$slashReplacement/$slash}"
}

#Alias::Exists(){
# local name="$1"
# local typeMatch=$(type "$name" 2> /dev/null || true)
Alias::Exists(){
local name="$1"
local typeMatch=$(type -t "$name" 2> /dev/null || true)
[[ "$typeMatch" == "alias" ]]
# echo "$typeMatch" | grep "function\|alias" &> /dev/null || return 1
# return 0
#}
}

Function::Exists(){
local name="$1"
Expand Down
2 changes: 1 addition & 1 deletion lib/system/06_system.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ System::Import() {
DEBUG subject=level3 Log "Trying to load from: ${libPath}"
[ ! -e "$libPath" ] && throw "Cannot import $libPath" && return 1

libPath="$(File.GetAbsolutePath "$libPath")"
libPath="$(File::GetAbsolutePath "$libPath")"

if [ -d "$libPath" ]; then
local file
Expand Down
35 changes: 1 addition & 34 deletions lib/system/oo/000_stub.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,4 @@
string::SanitizeForVariableName() {
String::SanitizeForVariableName() {
local type="$1"
echo "${type//[^a-zA-Z0-9]/_}"
}

## TODO:
#array::Assign() {
# local source="$1"
# local target="$2"
#
# eval "local -a tempMap=\"\$$__assign_paramNo\""
# local index
# local value
#
# ## copy the array / map item by item
# for index in "${!tempMap[@]}"
# do
# eval "$__assign_varName[\$index]=\"\${tempMap[\$index]}\""
# done
#
# unset index value tempMap
#}
#
#map::Assign() {
# ## TODO: test this
# eval "local -$(Variable::GetDeclarationFlagFromType '$__assign_varType') tempMap=\"\$$__assign_paramNo\""
# local index
# local value
#
# ## copy the array / map item by item
# for index in "${!tempMap[@]}"
# do
# eval "$__assign_varName[\$index]=\"\${tempMap[\$index]}\""
# done
#
# unset index value tempMap
#}
2 changes: 1 addition & 1 deletion lib/system/oo/00_variable.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace oo/variable
namespace oo/type

declare __declaration_type ## for Variable::ExportDeclarationAndTypeToVariables (?)

Expand Down
2 changes: 2 additions & 0 deletions lib/system/oo/01_pipe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ Pipe::Capture() {
Pipe::CaptureFaithful() {
IFS= read -r -d '' $1 || true
}

alias [pipe]="Pipe::Capture"
7 changes: 4 additions & 3 deletions lib/system/oo/02_type.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ Type::GetPrimitiveExtensionFingerprint() {
Type::CreateHandlerFunction() {
local variableName="$1"

## TODO - don't allow creating a handler if a command/function/alias of such name already exists
## unless it is a handler already (keep track?)

## declare method with the name of the var ##
eval "$variableName() {
Type::Handle $variableName \"\$@\";
}"
eval "$variableName() { Type::Handle $variableName \"\$@\"; }"
}

Type::TrapAndCreate() {
Expand Down
6 changes: 3 additions & 3 deletions lib/system/oo/03_stack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Type::RunCurrentStack() {

if [[ ! -z "$returnValueDefinition" && "$affectTheInitialVariable" == 'false' ]]
then
local -$(Variable::GetDeclarationFlagFromType returnValueType)"__self=$returnValueDefinition"
local -$(Variable::GetDeclarationFlagFromType $returnValueType) "__self=$returnValueDefinition"
variableName=__self
fi

# Log "Will assign: result=$(__return_self_and_result=true Type::ExecuteMethod "$type" "$variableName" "$method" "${params[@]}")"
local resultString=$(__return_self_and_result=true Type::ExecuteMethod "$type" "$variableName" "$method" "${params[@]}")

## TODO: some problem here sometimes
DEBUG Log "Result string: $resultString"
DEBUG Log "Result string: START | $resultString | END"

# echo everything before the first occurence of the separator
local echoed="${resultString%%$__return_separator*}"
Expand Down Expand Up @@ -188,7 +188,7 @@ Type::Handle() {

# Log index __${type}_property_names $(@get __${type}_property_names | __return_self_and_result=false array.indexOf ${1})

local typeSanitized=$(string::SanitizeForVariableName ${type})
local typeSanitized=$(String::SanitizeForVariableName ${type})
# local typeSanitized="${type//[^a-zA-Z0-9]/_}"

if Variable::Exists __${typeSanitized}_property_names &&
Expand Down
19 changes: 13 additions & 6 deletions lib/system/oo/04_classes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,25 @@ public() {
Type::DefineProperty public $class "$@"
}

Type::InitializeStatic() {
local name="$1"

class:$name

declare -Ag __oo_static_instance_${name}="$(Type::Construct $name)"
eval "${name}"'(){ '"Type::Handle __oo_static_instance_${name}"' "$@"; }'
}

Type::Initialize() {
local name="$1"
local isStatic="${2:-false}"

class:$name

if [[ "$isStatic" == 'true' || "$isStatic" == 'static' ]]
then
declare -Ag __oo_static_instance_${name}="$(Type::Construct $name)"

eval "${name}"'(){ '"Type::Handle __oo_static_instance_${name}"' "$@"; }'
Type::InitializeStatic "$name"
else
class:$name

## add alias for parameters
alias [$name]='_type=map Variable::TrapAssign local -A'

Expand All @@ -57,7 +64,7 @@ Type::Initialize() {

Type::Construct() {
local type="$1"
local typeSanitized=$(string::SanitizeForVariableName $type)
local typeSanitized=$(String::SanitizeForVariableName $type)
local assignToVariable="$2"

if [[ ! -z "${__constructor_recursion+x}" ]]
Expand Down
36 changes: 35 additions & 1 deletion lib/system/oo/04_primitive_map.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,38 @@ map.get() {
@return:value "${this[$1]}"
}

### /MAP
### /MAP


## TODO:
#array::Assign() {
# local source="$1"
# local target="$2"
#
# eval "local -a tempMap=\"\$$__assign_paramNo\""
# local index
# local value
#
# ## copy the array / map item by item
# for index in "${!tempMap[@]}"
# do
# eval "$__assign_varName[\$index]=\"\${tempMap[\$index]}\""
# done
#
# unset index value tempMap
#}
#
#map::Assign() {
# ## TODO: test this
# eval "local -$(Variable::GetDeclarationFlagFromType '$__assign_varType') tempMap=\"\$$__assign_paramNo\""
# local index
# local value
#
# ## copy the array / map item by item
# for index in "${!tempMap[@]}"
# do
# eval "$__assign_varName[\$index]=\"\${tempMap[\$index]}\""
# done
#
# unset index value tempMap
#}
4 changes: 2 additions & 2 deletions lib/system/oo/05_primitive_string.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
### STRING


string.toUpper() {
@resolve:this

@return:value "${this^^}"
}

string.=() {
@resolve:this
[string] value

@resolve:this

this="$value"

Expand Down
3 changes: 2 additions & 1 deletion lib/system/oo/06_primitive_integer.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
integer.=() {
@resolve:this
[string] value

@resolve:this

this="$value"

Expand Down
Loading

0 comments on commit 3add1bd

Please sign in to comment.