Compare commits

..

No commits in common. "13e6b1a3d3adca914d40e34d701988cef5950b94" and "66bfe1d2c4403895ff4d2e34fba654731f723c92" have entirely different histories.

5 changed files with 48 additions and 205 deletions

138
README.md
View file

@ -7,113 +7,57 @@ This idea was inspired from https://stackoverflow.com/a/40981277 in order to be
- .shn: **SH**ell **N**amespace
- .shc: **SH**ell **C**lass
## Syntax:
To declare objects, there exists 3 scripts.
- The main script in which we'll use our object (suggested extension: `.sh`)
- The namespace declaration script (suggested extension: `.shn`)
- The class declaration script (suggested extension: `.shc`)
The full example is available in the "example" directory.
## Example syntax:
### Declaring objects.
An object has a type name, properties and functions.
To declare a property, you can use the `property` function. For example, for an class named `Object`, you can declare a property name using:
```bash
property Object.name
```
**NOTE**: Bash doesn't have a typing system, so you cannot set property types.
`script.sh`:
This is the main script which will use our objects.
Class functions are declared the same way you would in bash, except it uses a prefix with object type. For example:
```bash
Object.print() {
echo "Example OOP from $(this.name)!"
}
```
As you can see here, you can access properties of the object using the `this` keyword in a function call.
Similarly, you can set properties using a `=` and value argument. For example:
```bash
this.name = "New name"
```
Objects can also have constructors which will be called at the creation of the object with arguments provided at the creation.
They are simply a function with the name `constructor`. They aren't mandatory for any object.
### Creating a namespace.
While you can import objects directly in the global namespace, it's recommanded to use a separate namespace file.
When you've created your namespace file, you can specify the name of the namespace using the `namespace` keyword:
```bash
namespace Example
```
You can then declare object classes using the `class` directive by specifying it's name and associated script file. For example:
```bash
class Object "Object.shc"
```
All objects created under this class will be accessible with namespace as prefix (here our Object class would be accessible under `Example.Object`).
Similarly, static classes can be declared using the `static_class` keyword.
```bash
static_class Static "Static.shc"
```
**NOTE**: Static classes can't have properties. As all static classes are global, you can declare global variables directly.
### Using objects.
Now that we've created our namespace, we will want to use it and our objects in our script.
First things first, we'll want to import the library `oop.sh`. Depending on where it's located, you will want to use a global variable indicating it's location.
```bash
. $OOP_ROOT/oop.sh # Import library.
importNamespace Example.shn
Example.Object t1 "Test"
Example.Object t2 "Example"
t1.print
t2.print
t1.name = "New name"
t1.print
```
After that, we'll want to import our namespace file with all it's classes prefixed in the namespace name.
`Example.shn`:
This file declares our namespace and all the objects within it.
```bash
importNamespace "Example.shn"
# Namespace declaration.
namespace Example
# If namespace is set to null (no argument), then the object will be declared globally.
# Otherwise, the object will be declared within the namespace.
# Object declaration, from class name to file name.
class Object "Object.shc"
```
After that you can declare the object using the following syntax: `<ObjectType> <variableName> [constructor arguments...]`. For example:
`Object.shc`:
This file will contain the object code.
```bash
Example.Object obj1 "Test"
# Property declaration
property Object.name
# Optional constructor.
Object.constructor() {
Object.name = $1
}
# Example function
Object.print() {
echo "Example OOP from $($this.name)!"
}
```
You can then call it's functions.
```bash
$obj1.print
obj1.print
```
**NOTE**: The $ is not mandatory, but is recommanded for clarity.
... or access and edit it's properties.
```bash
name=$(obj1.name)
obj1.name = "New name"
```
You can store objects in variables as a string. For example, you can have have objects as class arguments, function returs or arrays of objects like this:
```bash
Example.Object obj1 "First Object"
Example.Object obj2 "Second Object"
objs=($obj1 $obj2)
${objs[0]}.print
${objs[1]}.print
```
You can also access the static classes by using their class type directly. For example:
```bash
Example.Static.print "Example text"
```
If you find that using the namespace everytime is a bit cumbersome, you can use the `using` keyword to alias all classes of a namespace into the global namespace.
Example usage:
```bash
using Example
Object usingObj "New"
$usingObj.print
```
**NOTE**: When `using` a namespace which contains static classes, please note that the static class file will be re-imported.

View file

@ -5,6 +5,3 @@ namespace Example
# Object declaration, from class name to file name.
class Object "Object.shc"
# Static class declaration
static_class Static "Static.shc"

View file

@ -1,6 +0,0 @@
# Function declaration.
# NOTE: Properties can't be declared for static classes
Static.print() {
echo "$@"
}

View file

@ -3,31 +3,14 @@ OOP_ROOT=..
. $OOP_ROOT/oop.sh # Import library.
# Import library
importNamespace Example.shn
# OOP Example
Example.Object obj1 "First Object"
Example.Object obj2 "Second Object"
Example.Object t1 "Test"
Example.Object t2 "Example"
$obj1.print
# The $ is not mandatory, but is recommanded.
obj2.print
t1.print
t2.print
objs=($obj1 $obj2)
${objs[0]}.print
${objs[1]}.print
t1.name = "New name"
$obj1.name = "New name"
$obj1.print
# Static example
Example.Static.print "Example text"
# Using directive.
using Example
Object usingObj "New"
$usingObj.print
t1.print

77
oop.sh
View file

@ -3,16 +3,9 @@
# This file contains all functions required to create a namespace.
# Internal variables are marked with a beginning underscore, like in most other languages.
# Signatures are a list of arguments.. Those within <> are mandatory, the ones within [] are optional.
# Namespace related variables.
_namespace=""
_namespacePath=$(realpath $(dirname ${BASH_SOURCE[0]}))
# This dictionnary saves all classes for each namespace so they can be retreived and aliased.
declare -Ag _namespacesClasses
# This dictionnary links all files for static namespaces.
declare -Ag _namespacesStaticClasses
# Namespace declaration.
# Signature: ([string namespaceName])
namespace() {
@ -20,10 +13,6 @@ namespace() {
}
# Imports a namespace into the current shell.
# It saves the path of the file so that relative paths can be
# properly resolved.
# For example, if the object Object exists within namespace Example, it
# will be accessible with "Example.Object".
# Signature: (<string namespaceFile>)
importNamespace() {
namespaceFile=$1
@ -32,33 +21,6 @@ importNamespace() {
. $namespaceFile
}
# Aliases the classes in global namespace.
# For example, if the object Object exists within namespace Example, it
# will be accessible with "Example.Object" and "Object".
# Signature: (<string namespaceName>)
using() {
namespaceName=$1
# Import static classes
if [ "${_namespacesStaticClasses[$namespaceName]}" != "" ]; then
oldNamespace=_namespace
namespace # Reset namespace
# Split all
staticClasses=$(echo "${_namespacesStaticClasses[$namespaceName]}" | tr ";" "\n")
for cl in $staticClasses; do
parts=$(echo "$cl" | tr ":" "\n")
static_class ${parts[0]} ${parts[1]}
done
namespace $oldNamespace
fi
# Import static classes
if [ "${_namespacesClasses[$namespaceName]}" != "" ]; then
classes=$(echo "${_namespacesClasses[$namespaceName]}" | tr ";" "\n")
for type in $classes; do
eval "$type() { $namespaceName.$type \$@; }"
done
fi
}
# Creates an object instance.
# Signature: (<string type>, <string associatedFile>, <string variableName>, [string[] constructorArguments])
_createObject() {
@ -75,8 +37,6 @@ _createObject() {
. <(sed s/this\\./$varName./g <(sed s/$type\\./$varName./g $associatedFile))
# Call the constructor
eval "$varName.constructor $constructorArguments"
# alias the "varName" variable to itself, so that it can be used and transmitted in other variables (e.g: $varName.name would alias to varName.name)
eval "$varName='$varName'"
}
# Object creation.
@ -94,42 +54,7 @@ class() {
associatedFile="$_namespacePath/$associatedFile"
fi
# Declares a new function for object initialisation.
eval "$objFullName() { _createObject $type $associatedFile \$@; }"
# Save the class in the dictionnary for reference.
if [ "$_namespace" != "" ]; then
if [ "${_namespacesClasses[$_namespace]}" == "" ]; then
_namespacesClasses[$_namespace]=$type
else
_namespacesClasses[$_namespace]="${_namespacesClasses[$_namespace]};$type"
fi
fi
}
# Static class creation
# Signature: (<string type>, <string associatedFile>)
static_class() {
type=$1 # Type of the object as declared within the file.
associatedFile=$2
objFullName=$type # Type of the object referenced elsewhere
if [ "$_namespace" != "" ]; then
objFullName="${_namespace}.$type"
fi
if [ ${associatedFile::1} != "/" ]; then # Relative path, we save only the absolute path
associatedFile="$_namespacePath/$associatedFile"
fi
# Imports the file and replace all "<Type>." with the variable name.
. <(sed s/this\\./$objFullName./g <(sed s/$type\\./$objFullName./g $associatedFile))
# Save the class in the dictionnary for reference.
if [ "$_namespace" != "" ]; then
if [ "${_namespacesStaticClasses[$_namespace]}" == "" ]; then
_namespacesStaticClasses[$_namespace]="$type:$associatedFile"
else
_namespacesStaticClasses[$_namespace]="${_namespacesStaticClasses[$_namespace]};$type:$associatedFile"
fi
fi
eval "$objFullName() { _createObject $type $associatedFile \$@; }"
}
# Associated function for properties