-
Notifications
You must be signed in to change notification settings - Fork 0
/
loo.ps1
67 lines (61 loc) · 1.6 KB
/
loo.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
$filename = $args[0]
<#
Print out the parameter passed
in along with the function
#>
function print($data){
Write-Output $data
}
<#
The main language parser which parser
and executes the code based on the tokens
#>
function language($data){
# the value incremented and decremented
# based on the tokens
[int]$character_value = 0
for($index=0; $index -lt $data.Length; $index++){
[string]$character = $data[$index]
for($j=0; $j -lt $character.Length; $j++){
[string]$char = $character[$j]
if($char -eq "+"){
$character_value++
} elseif($char -eq "-"){
$character_value--
} elseif($char -eq "#"){
[char]$out = $character_value
Write-Output $out
} elseif($char -eq ";"){
exit 1
} else {
throw("Unexpected character: $char")
}
}
}
}
<#
Check file existence and read
the file if the file exists
#>
function readFileContent($filename){
if (Test-Path $filename -PathType leaf) {
$file = Get-Item $filename
if($file.Extension -eq ".loo"){
$content = Get-Content $filename
language($content)
} else {
$extension = $file.Extension
throw("Unexpected file extension $extension, expected .loo")
}
} else {
throw("File does not exist")
}
}
function validate_filename($filename) {
if($filename){
readFileContent($filename)
} else {
throw("No filename provided")
}
}
validate_filename($filename)