schaefer 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/.DS_Store +0 -0
  2. data/lib/schaefer/.DS_Store +0 -0
  3. data/lib/schaefer/.interpreter.rb.swp +0 -0
  4. data/lib/schaefer/Library/.split.sch.swp +0 -0
  5. data/lib/schaefer/Library/additionOperator.sch +1 -1
  6. data/lib/schaefer/Library/append.sch +21 -1
  7. data/lib/schaefer/Library/assoc.sch +16 -0
  8. data/lib/schaefer/Library/deleteAt.sch +13 -0
  9. data/lib/schaefer/Library/each.sch +43 -0
  10. data/lib/schaefer/Library/first.sch +11 -0
  11. data/lib/schaefer/Library/index.sch +20 -0
  12. data/lib/schaefer/Library/integer.sch +8 -0
  13. data/lib/schaefer/Library/isA.sch +16 -0
  14. data/lib/schaefer/Library/keyfor.sch +19 -0
  15. data/lib/schaefer/Library/map.sch +34 -0
  16. data/lib/schaefer/Library/pair.sch +12 -0
  17. data/lib/schaefer/Library/pull.sch +8 -0
  18. data/lib/schaefer/Library/return.sch +10 -0
  19. data/lib/schaefer/Library/reverse.sch +7 -0
  20. data/lib/schaefer/Library/set.sch +15 -0
  21. data/lib/schaefer/Library/shift.sch +2 -1
  22. data/lib/schaefer/Library/split.sch +40 -0
  23. data/lib/schaefer/Library/string.sch +8 -0
  24. data/lib/schaefer/environment.rb +3 -0
  25. data/lib/schaefer/interpreter.rb +4 -3
  26. data/lib/schaefer/parser.rb +12 -0
  27. data/lib/schaefer/version.rb +1 -1
  28. metadata +22 -33
  29. data/Examples/.DS_Store +0 -0
  30. data/Examples/Arithmatic/exampleAdd.sch +0 -2
  31. data/Examples/Arithmatic/exampleDivision.sch +0 -1
  32. data/Examples/Arithmatic/exampleMultipy.sch +0 -3
  33. data/Examples/Arithmatic/exampleSubtraction.sch +0 -1
  34. data/Examples/Commenting/exampleComment.sch +0 -12
  35. data/Examples/FlowControl/exampleDo.sch +0 -18
  36. data/Examples/FlowControl/exampleGreater.sch +0 -17
  37. data/Examples/FlowControl/exampleIf.sch +0 -18
  38. data/Examples/FlowControl/exampleLesser.sch +0 -13
  39. data/Examples/Functions/exampleFunction.sch +0 -18
  40. data/Examples/Functions/exampleFunction2.sch +0 -10
  41. data/Examples/Input:Output/exampleDisplay.sch +0 -6
  42. data/Examples/Input:Output/exampleInput.sch +0 -13
  43. data/Examples/Input:Output/exampleWrite.sch +0 -6
  44. data/Examples/Lists/exampleAppend.sch +0 -9
  45. data/Examples/Lists/exampleConstruct.sch +0 -27
  46. data/Examples/Lists/exampleLength.sch +0 -9
  47. data/Examples/Lists/exampleList.sch +0 -7
  48. data/Examples/Lists/exampleListFirst.sch +0 -10
  49. data/Examples/Lists/exampleQuote.sch +0 -7
  50. data/Examples/Lists/exampleShift.sch +0 -11
  51. data/Examples/Lists/exampleUnshift.sch +0 -18
  52. data/Examples/Practical/each.sch +0 -22
  53. data/Examples/Practical/fib.sch +0 -44
  54. data/lib/schaefer/Library/listFirst.sch +0 -7
data/.DS_Store CHANGED
Binary file
Binary file
@@ -2,6 +2,6 @@
2
2
  (native_function "
3
3
  Proc.new do |arguments, interpreter|
4
4
  tmp = arguments.map {|item| interpreter.evaluate(item)}
5
- tmp.inject {|sum, n| sum + n.to_i}
5
+ tmp.inject {|sum, n| sum + n}
6
6
  end
7
7
  "))
@@ -1,6 +1,26 @@
1
1
  (define append
2
2
  (native_function "
3
3
  Proc.new() do |arguments, interpreter|
4
- arguments.map {|x| interpreter.evaluate(x) }.flatten
4
+ arg1 = interpreter.evaluate(arguments[0])
5
+ if arg1.is_a? Array
6
+ arguments.map {|x| interpreter.evaluate(x) }.flatten
7
+ elsif arg1.is_a? Hash
8
+ result = arg1
9
+ args = arguments.dup
10
+ args.shift
11
+ args.each do |argument|
12
+ argument = interpreter.evaluate(argument)
13
+ if argument.is_a?(Hash)
14
+ result.merge!(argument)
15
+ else
16
+ if argument.count != 2
17
+ raise 'When appendind to hash each argument must be a pair'
18
+ else
19
+ result[argument[0]] = argument[1]
20
+ end
21
+ end
22
+ end
23
+ result
24
+ end
5
25
  end
6
26
  "))
@@ -0,0 +1,16 @@
1
+ (define assoc
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ result = {}
5
+ arguments.each do |x|
6
+ element = interpreter.evaluate(x)
7
+ if element.count != 2
8
+ raise 'Each element must be a key value pair!'
9
+ else
10
+ result[element[0]] = element[1]
11
+ end
12
+ end
13
+ result
14
+ end
15
+ "))
16
+
@@ -0,0 +1,13 @@
1
+ (define deleteAt
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ arg1 = interpreter.evaluate(arguments[0])
5
+ arg2 = interpreter.evaluate(arguments[1])
6
+ if arg1.is_a?(Array)
7
+ arg1.delete_at(arg2)
8
+ elsif arg1.is_a?(Hash)
9
+ arg1.delete(arg2)
10
+ end
11
+ arg1
12
+ end
13
+ "))
@@ -0,0 +1,43 @@
1
+ (define each
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ list = interpreter.evaluate(arguments[0])
5
+ if list.is_a? Array
6
+ formals = arguments[1]
7
+ elsif list.is_a? Hash
8
+ formals = arguments[1,2]
9
+ end
10
+ body = arguments.slice(2, arguments.length)
11
+
12
+ raise 'Error: Each function takes a minimum of three arguments!' unless arguments.length > 2
13
+ raise 'Error: Argument one must be a list or hash!' unless list.is_a?(Array) || list.is_a?(Hash)
14
+ raise 'Error: Formal cannot be defined as a string!' if formals.is_a? String
15
+
16
+ sub_env = Schaefer::Environment.new(interpreter.current_environment)
17
+
18
+ executeList = Proc.new do |body, environment, interpreter|
19
+ interpreter.current_environment = environment
20
+ result = nil
21
+ if list.is_a?(Array)
22
+ list.each do |element|
23
+ interpreter.current_environment.define(formals, element)
24
+ body.each do |expression|
25
+ result = interpreter.evaluate(expression)
26
+ end
27
+ end
28
+ else
29
+ list.each do |key, value|
30
+ interpreter.current_environment.define(formals[0], key)
31
+ interpreter.current_environment.define(formals[1], value)
32
+ body.each do |expression|
33
+ result = interpreter.evaluate(expression)
34
+ end
35
+ end
36
+ end
37
+ interpreter.current_environment = environment.parent
38
+ result
39
+ end
40
+
41
+ executeList.call(body, sub_env, interpreter)
42
+ end
43
+ "))
@@ -0,0 +1,11 @@
1
+ (define first
2
+ (native_function "
3
+ Proc.new do |argument, interpreter|
4
+ arg1 = interpreter.evaluate(argument[0])
5
+ if arg1.is_a? Array
6
+ arg1[0]
7
+ elsif arg1.is_a? String
8
+ arg1[0,1]
9
+ end
10
+ end
11
+ "))
@@ -0,0 +1,20 @@
1
+ (define index
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ arg1 = interpreter.evaluate(arguments[0])
5
+ arg2 = interpreter.evaluate(arguments[1])
6
+ index = []
7
+ i = 0
8
+ arg1.each do |element|
9
+ if arg2 == element
10
+ index << i
11
+ end
12
+ i += 1
13
+ end
14
+ if index.length == 1
15
+ index[0]
16
+ else
17
+ index
18
+ end
19
+ end
20
+ "))
@@ -0,0 +1,8 @@
1
+ (define integer
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ 9
5
+ end
6
+ "))
7
+
8
+ (define int integer)
@@ -0,0 +1,16 @@
1
+ (define isA?
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ if arguments.count != 2
5
+ raise 'The isA? comparitor accepts two arguments'
6
+ else
7
+ arg1 = interpreter.evaluate(arguments[0])
8
+ arg2 = interpreter.evaluate(arguments[1])
9
+ if arg1.is_a?(arg2.class)
10
+ true
11
+ else
12
+ false
13
+ end
14
+ end
15
+ end
16
+ "))
@@ -0,0 +1,19 @@
1
+ (define keyfor
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ argument1 = interpreter.evaluate(arguments[0])
5
+ argument2 = interpreter.evaluate(arguments[1])
6
+ index = []
7
+ i = 0
8
+ argument1.each do |key, value|
9
+ if argument2 == value
10
+ index << key
11
+ end
12
+ end
13
+ if index.length == 1
14
+ index[0]
15
+ else
16
+ index
17
+ end
18
+ end
19
+ "))
@@ -0,0 +1,34 @@
1
+ (define map
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ list = interpreter.evaluate(arguments[0])
5
+ formals = arguments[1]
6
+ body = arguments.slice(2, arguments.length)
7
+
8
+ raise 'Error: Map function takes a minimum of three arguments!' unless arguments.length > 2
9
+ raise 'Error: Argument one must be a list!' unless list.is_a? Array
10
+ raise 'Error: Formal cannot be defined as a string!' if formals.is_a? String
11
+
12
+ if formals.is_a? Array
13
+ raise 'Error: Map function takes one formal!' if formals.length != 1
14
+ formals = formals[0]
15
+ end
16
+
17
+ sub_env = Schaefer::Environment.new(interpreter.current_environment)
18
+
19
+ executeList = Proc.new do |body, environment, interpreter|
20
+ interpreter.current_environment = environment
21
+ result = []
22
+ list.each do |element|
23
+ interpreter.current_environment.define(formals, element)
24
+ body.each do |expression|
25
+ result << interpreter.evaluate(expression)
26
+ end
27
+ end
28
+ interpreter.current_environment = environment.parent
29
+ result
30
+ end
31
+
32
+ executeList.call(body, sub_env, interpreter)
33
+ end
34
+ "))
@@ -0,0 +1,12 @@
1
+ (define pair
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ if arguments.count != 2
5
+ raise 'Pair function only takes two arguments: key and value'
6
+ else
7
+ arguments.map do |x|
8
+ interpreter.evaluate(x)
9
+ end
10
+ end
11
+ end
12
+ "))
@@ -0,0 +1,8 @@
1
+ (define pull
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ arg1 = interpreter.evaluate(arguments[0])
5
+ arg2 = interpreter.evaluate(arguments[1])
6
+ arg1[arg2]
7
+ end
8
+ "))
@@ -0,0 +1,10 @@
1
+ (define return
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ if arguments.count != 1
5
+ raise 'Return function takes one argument'
6
+ else
7
+ interpreter.evaluate(arguments[0])
8
+ end
9
+ end
10
+ "))
@@ -0,0 +1,7 @@
1
+ (define reverse
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ arg1 = interpreter.evaluate(arguments[0])
5
+ arg1.reverse
6
+ end
7
+ "))
@@ -0,0 +1,15 @@
1
+ (define set
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ if arguments.count != 3
5
+ raise 'Set function requires 3 arguments!'
6
+ else
7
+ arg1 = interpreter.evaluate(arguments[0])
8
+ if arg1.is_a?(Array) || arg1.is_a?(Hash)
9
+ arg2 = interpreter.evaluate(arguments[1])
10
+ arg3 = interpreter.evaluate(arguments[2])
11
+ arg1[arg2] = arg3
12
+ end
13
+ end
14
+ end
15
+ "))
@@ -2,6 +2,7 @@
2
2
  (native_function "
3
3
  Proc.new do |argument, interpreter|
4
4
  list = interpreter.evaluate(argument[0])
5
- list.slice(1, list.length)
5
+ list.shift
6
+ list
6
7
  end
7
8
  "))
@@ -0,0 +1,40 @@
1
+ (define splitString
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ arg1 = interpreter.evaluate(arguments[0])
5
+ if arg1.is_a? String
6
+ arg1.split
7
+ else
8
+ raise 'splitString function takes a string as an argument.'
9
+ end
10
+ end
11
+ "))
12
+
13
+ (define split
14
+ (function (x)
15
+ (define keys (list))
16
+ (define values (list))
17
+ (if (isA? x (assoc))
18
+ (do
19
+ (each x k v
20
+ (define keys (append keys k))
21
+ (define values (append values v))
22
+ (return (assoc (pair (quote keys) keys) (pair (quote values) values)))
23
+ )
24
+ )
25
+ (if (isA? x (list))
26
+ (do
27
+ (each x element
28
+ (define keys (append keys (index x element)))
29
+ (define values (append values element))
30
+ (return (assoc (pair (quote keys) keys) (pair (quote values) values)))
31
+ )
32
+ )
33
+ (if (isA? x (str))
34
+ (splitString x)
35
+ ()
36
+ )
37
+ )
38
+ )
39
+ )
40
+ )
@@ -0,0 +1,8 @@
1
+ (define string
2
+ (native_function "
3
+ Proc.new do |arguments, interpreter|
4
+ ''
5
+ end
6
+ "))
7
+
8
+ (define str string)
@@ -16,5 +16,8 @@ module Schaefer
16
16
  def define(name, value)
17
17
  @table[name] = value
18
18
  end
19
+
20
+ def removeDefinition(name)
21
+ end
19
22
  end
20
23
  end
@@ -37,9 +37,10 @@ module Schaefer
37
37
 
38
38
  def loadLibrary
39
39
  pattern = File.join(File.dirname(__FILE__), "Library", "*.sch")
40
-
41
- Dir[pattern].each do |file|
42
- run(File.read(file))
40
+ Dir[pattern].each do |item|
41
+ File.open(item) do |file|
42
+ run(file.read)
43
+ end
43
44
  end
44
45
  end
45
46
  end
@@ -57,6 +57,10 @@ class Parser
57
57
  return isMatch?(string, /"([^"\\]|\\.)*"/)
58
58
  end
59
59
 
60
+ def isComma?(string)
61
+ return isMatch?(string, /.*,.*/)
62
+ end
63
+
60
64
  def convertTokens(tokens)
61
65
  convertedTokens = []
62
66
  tokens.each do |token|
@@ -65,6 +69,7 @@ class Parser
65
69
  convertedTokens << token.to_i and next if isInteger?(token)
66
70
  convertedTokens << token.to_sym and next if isSymbol?(token)
67
71
  convertedTokens << eval(token) and next if isString?(token)
72
+ convertedTokens << token and next if isComma?(token)
68
73
  raise Exception, "Unrecognized token: #{token}"
69
74
  end
70
75
  return convertedTokens
@@ -118,6 +123,13 @@ class Parser
118
123
  end
119
124
  end
120
125
  "(" + mapped.join(" ") + ")"
126
+ elsif (data.is_a?(Hash))
127
+ mapped = data.map do |key, value|
128
+ key = toToken(key)
129
+ value = toToken(value)
130
+ key + "=>" + value
131
+ end
132
+ "(" + mapped.join(" ") + ")"
121
133
  else
122
134
  data.to_s
123
135
  end
@@ -1,3 +1,3 @@
1
1
  module Schaefer
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schaefer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tommy Schaefer
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-29 00:00:00 Z
18
+ date: 2012-06-23 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Schaefer is a small programming language deigned to show how to build a programming language. Running the command 'schaefer' begins console based execution of code and passing a file name will execute the file. SchaeferScript is a scheme-style language where the core functions are written in ruby and other functions written in the language itself. You can access the source for SchaeferScript at https://github.com/tommyschaefer/SchaeferScript.
@@ -32,31 +32,6 @@ files:
32
32
  - .DS_Store
33
33
  - .gitignore
34
34
  - .gitignore~
35
- - Examples/.DS_Store
36
- - Examples/Arithmatic/exampleAdd.sch
37
- - Examples/Arithmatic/exampleDivision.sch
38
- - Examples/Arithmatic/exampleMultipy.sch
39
- - Examples/Arithmatic/exampleSubtraction.sch
40
- - Examples/Commenting/exampleComment.sch
41
- - Examples/FlowControl/exampleDo.sch
42
- - Examples/FlowControl/exampleGreater.sch
43
- - Examples/FlowControl/exampleIf.sch
44
- - Examples/FlowControl/exampleLesser.sch
45
- - Examples/Functions/exampleFunction.sch
46
- - Examples/Functions/exampleFunction2.sch
47
- - Examples/Input:Output/exampleDisplay.sch
48
- - Examples/Input:Output/exampleInput.sch
49
- - Examples/Input:Output/exampleWrite.sch
50
- - Examples/Lists/exampleAppend.sch
51
- - Examples/Lists/exampleConstruct.sch
52
- - Examples/Lists/exampleLength.sch
53
- - Examples/Lists/exampleList.sch
54
- - Examples/Lists/exampleListFirst.sch
55
- - Examples/Lists/exampleQuote.sch
56
- - Examples/Lists/exampleShift.sch
57
- - Examples/Lists/exampleUnshift.sch
58
- - Examples/Practical/each.sch
59
- - Examples/Practical/fib.sch
60
35
  - Gemfile
61
36
  - LICENSE
62
37
  - README.md
@@ -67,29 +42,43 @@ files:
67
42
  - lib/schaefer.rb
68
43
  - lib/schaefer/.DS_Store
69
44
  - lib/schaefer/.environment.rb.swp
45
+ - lib/schaefer/.interpreter.rb.swp
70
46
  - lib/schaefer/Library/.DS_Store
47
+ - lib/schaefer/Library/.split.sch.swp
71
48
  - lib/schaefer/Library/additionOperator.sch
72
49
  - lib/schaefer/Library/append.sch
50
+ - lib/schaefer/Library/assoc.sch
73
51
  - lib/schaefer/Library/comment.sch
74
52
  - lib/schaefer/Library/construct.sch
53
+ - lib/schaefer/Library/deleteAt.sch
75
54
  - lib/schaefer/Library/display.sch
76
55
  - lib/schaefer/Library/divisionOperator.sch
77
56
  - lib/schaefer/Library/do.sch
78
- - lib/schaefer/Library/do.sch~
57
+ - lib/schaefer/Library/each.sch
79
58
  - lib/schaefer/Library/equalTo.sch
80
- - lib/schaefer/Library/equalTo.sch~
59
+ - lib/schaefer/Library/first.sch
81
60
  - lib/schaefer/Library/function.sch
82
61
  - lib/schaefer/Library/if.sch
83
- - lib/schaefer/Library/if.sch~
62
+ - lib/schaefer/Library/index.sch
84
63
  - lib/schaefer/Library/input.sch
64
+ - lib/schaefer/Library/integer.sch
65
+ - lib/schaefer/Library/isA.sch
85
66
  - lib/schaefer/Library/isgreater.sch
86
67
  - lib/schaefer/Library/islesser.sch
68
+ - lib/schaefer/Library/keyfor.sch
87
69
  - lib/schaefer/Library/length.sch
88
70
  - lib/schaefer/Library/list.sch
89
- - lib/schaefer/Library/listFirst.sch
71
+ - lib/schaefer/Library/map.sch
90
72
  - lib/schaefer/Library/multiplicationOperator.sch
73
+ - lib/schaefer/Library/pair.sch
74
+ - lib/schaefer/Library/pull.sch
91
75
  - lib/schaefer/Library/quote.sch
76
+ - lib/schaefer/Library/return.sch
77
+ - lib/schaefer/Library/reverse.sch
78
+ - lib/schaefer/Library/set.sch
92
79
  - lib/schaefer/Library/shift.sch
80
+ - lib/schaefer/Library/split.sch
81
+ - lib/schaefer/Library/string.sch
93
82
  - lib/schaefer/Library/subtractionOperator.sch
94
83
  - lib/schaefer/Library/tonum.sch
95
84
  - lib/schaefer/Library/tostr.sch
Binary file
@@ -1,2 +0,0 @@
1
- (add 1 2 3 4 5)
2
-
@@ -1 +0,0 @@
1
- (write (/ (/ 100 2) 2))
@@ -1,3 +0,0 @@
1
- (display "2*4 = ")
2
- (write (* 2 4))
3
-
@@ -1 +0,0 @@
1
- (write (- (- 100 (- 100 50) 25) 20))
@@ -1,12 +0,0 @@
1
- Commenting can be acheaved by simply typing outside of parenthesees. There
2
- are a few things to be careful of though. Commas and some other symbols are
3
- not yet recognized in the parser so they return wierd values. This will be
4
- fixed in the next release. Also be careful not to include any parenthasees
5
- because otherwise they will be interpreted as commands!
6
-
7
-
8
- (define test
9
- (function ()
10
- (## When inside of parenthasees just use the double hash)
11
- (write "hello")))
12
- (test)
@@ -1,18 +0,0 @@
1
- Do is a function that steps through a list of commands. In some functions such as if as seen below
2
- a number of commands must be executed within a single set of parenthasees. This task is accomplished
3
- with the do function.
4
- _____________________________________________________________________________________________________
5
-
6
-
7
- (define one 1)
8
- (define won 1)
9
-
10
- (if (equal? one won)
11
- (do
12
- (display "1 and 1 are equal")
13
- (write " and thats a fact"))
14
- (write "1 and 1 are not equal!"))
15
-
16
-
17
- -------------------------------------------------------------------------------------
18
- This example will return "1 and 1 are equal and thats a fact" followed by a new line.
@@ -1,17 +0,0 @@
1
- The isGreater? function allows you to test weather one number is greater than the other.
2
- The greater check takes two arguments and compares the first to the seccond.
3
- ----------------------------------------------------------------------------------------
4
-
5
-
6
- (define one 1)
7
- (define two 2)
8
-
9
- (if(isGreater? one two)
10
- (write "1 > 2")
11
- (write "1 < 2"))
12
-
13
-
14
-
15
- -----------------------------------------------------------------------------------------------
16
- In this example the correct return is "1 < 2". Because 1 is being compared to 2 isGreater? will
17
- return false.
@@ -1,18 +0,0 @@
1
- The if function takes three arguments. The first is the comparison which will either return
2
- true or false. The seccond is the true case which will be executed if the result of the
3
- comparison is true. And the last is the false case which will be executed if the result
4
- of the comparisson is false.
5
- --------------------------------------------------------------------------------------------
6
-
7
-
8
- (define one 1)
9
- (define won 1)
10
- (define two 2)
11
-
12
- (if (= one won)
13
- (write "1 = 1")
14
- (write "1 != 1"))
15
-
16
- (if (equal? one two)
17
- (write "1 = 2")
18
- (write "1 != 2"))
@@ -1,13 +0,0 @@
1
- The isLesser? is a comparitive function that takes two arguments.
2
- TIf the first is lesser than the seccond the function will return
3
- true and if the first is greater than the seccond the function
4
- will return false
5
- ------------------------------------------------------------------
6
-
7
-
8
- (define one 1)
9
- (define two 2)
10
-
11
- (if(isLesser? one two)
12
- (write "1 < 2")
13
- (write "1 > 2"))
@@ -1,18 +0,0 @@
1
- The function function has many uses. When passed no variables it can be used
2
- to make your definition executable and allowing loops. In this example you
3
- are prompted to enter a password until you answer correctly. This demonstrates
4
- using empty functions to generate loops and executable definitions.
5
- -------------------------------------------------------------------------------
6
-
7
-
8
- (define program
9
- (function ()
10
- (display "Enter password (Hint its password): ")
11
- (define response (input))
12
- (if (= response "password")
13
- (write "Great Job!")
14
- (do
15
- (write "Im sorry. Thats incorrect!")
16
- (program)))))
17
-
18
- (program)
@@ -1,10 +0,0 @@
1
- This example demonstrates how functions can be used by passing them arguments.
2
- Using functions in this mannor allows you to create new functions much like
3
- some that define this language!
4
- ------------------------------------------------------------------------------
5
-
6
-
7
- (define double
8
- (function (x) (+ x x)))
9
-
10
- (write (double 5))
@@ -1,6 +0,0 @@
1
- The display command takes one argument. The function then evaluates that argument and displays
2
- it to the screen without a trailing newline.
3
- ----------------------------------------------------------------------------------------------
4
-
5
-
6
- (display "hello")
@@ -1,13 +0,0 @@
1
- The input command takes no arguments. In allows for user input up until a newline
2
- is detected. The function then returns a string of the input excluding the newline
3
- character.
4
- ----------------------------------------------------------------------------------
5
-
6
-
7
- (display "Enter password (Hint: its password): ")
8
- (define result (input))
9
-
10
- (if(= result "password")
11
- (write "Excellent!")
12
- (write "Wrong!"))
13
-
@@ -1,6 +0,0 @@
1
- The write command is nearly identical to the display command. It takes one argument then evaluates
2
- that argument and displays it to the screen with a trailing newline.
3
- --------------------------------------------------------------------------------------------------
4
-
5
-
6
- (write "hello")
@@ -1,9 +0,0 @@
1
- The append command takes an unlimited ammount of arguments. It works by constructing
2
- a single list given multiple in the same order that they are specified.
3
- ------------------------------------------------------------------------------------
4
-
5
-
6
- (define l1 (quote (1 2 3 4 5)))
7
- (define l2 (quote (6 7 8 9 0)))
8
-
9
- (write (append l1 l2))
@@ -1,27 +0,0 @@
1
- Construct takes two arguments. By performing a construct argument 1 is placed before argument 3
2
- and the result is not flattened. This has the effect of creating multi-dimensional arrays. Notice
3
- that the length reflects the number of elements with a sub-list contributing one.
4
- -------------------------------------------------------------------------------------------------
5
-
6
-
7
- (define list1 (list 1 2 3 4 5))
8
- (define list2 (list 6 7 8 9 0))
9
-
10
- (display "List1: ")
11
- (write list1)
12
-
13
- (display "List2: ")
14
- (write list2)
15
-
16
- (define ap (append list1 list2))
17
- (define con (construct list1 list2))
18
-
19
- (display "Appended List1-List2: ")
20
- (display ap)
21
- (display " -Length: ")
22
- (write (length ap))
23
-
24
- (display "Constructed List1-List2: ")
25
- (display con)
26
- (display " -Length: ")
27
- (write (length con))
@@ -1,9 +0,0 @@
1
- The length function takes one argument. The argument is then evaluated
2
- and the length of the list is returned.
3
- ----------------------------------------------------------------------
4
-
5
-
6
- (define l1 (list 0 1 2 3 4 5 6 7))
7
- (define l2 (list 8 9 10))
8
-
9
- (write (length (append l1 l2)))
@@ -1,7 +0,0 @@
1
- A list is a data type similar to an array in that it is a grouping of values
2
- but different in that they are not tied to any keys. When lists are created
3
- with the list function each element is evaluated before it is stored.
4
- ----------------------------------------------------------------------------
5
-
6
-
7
- (write (list 1 2 3 4 5))
@@ -1,10 +0,0 @@
1
- The listFirst command takes one list as an argument. It then returns the first element
2
- of the provided list.
3
- --------------------------------------------------------------------------------------
4
-
5
-
6
- (define l1 (quote (1 2 3 4 5)))
7
-
8
- (write l1)
9
- (display "First: ")
10
- (write (listFirst l1))
@@ -1,7 +0,0 @@
1
- By using quote you can create lists without evaluating each element. When you create a list
2
- using the list keyword each element is evaluated and symbols are interpreted as variables.
3
- By quoting the contents of a list are not evaluated and symbols are left unevaluated.
4
- --------------------------------------------------------------------------------------------
5
-
6
-
7
- (write (quote (1 2 3 4 5 bob)))
@@ -1,11 +0,0 @@
1
- The shift function takes one list as the argument. The function removes the first element from
2
- the list and returns the list again. A combination of listFirst and shift can be used to get
3
- a different values from a list.
4
- -----------------------------------------------------------------------------------------------
5
-
6
-
7
- (define l1 (list 1 2 3 4 5))
8
- (display "List: ")
9
- (write l1)
10
- (display "List Post-Shift: ")
11
- (write (shift l1))
@@ -1,18 +0,0 @@
1
- The unshift function takes an unlimited number of arguments. The unshift function
2
- takes an array provided in argument 1 and evaluates the subsequent arguments. It
3
- then adds the result before the first list in the order they were provided and
4
- then flattens the list. Eg. Argument 1 is the list 1 2 3. Argument 2 is the list
5
- 3 4 5. Argument 3 is 5 6 7. The list resulting from unshifting under these conditions
6
- would be 3 4 5 5 6 7 1 2 3.
7
- -------------------------------------------------------------------------------------
8
-
9
-
10
- (define list1 (list -4 -3 -2 -1 0))
11
- (define list2 (list 1 2 3 4 5))
12
- (define list3 (list 6 7 8 9 10))
13
-
14
- (display "Append: ")
15
- (write (append list1 list2 list3))
16
-
17
- (display "Unshift: ")
18
- (write (unshift list3 list1 list2))
@@ -1,22 +0,0 @@
1
- THe each example demonstrates how a typical array-each would be written in
2
- SchaeferScript. Rather that just writing the element that you are currently
3
- at you could do any task.
4
- ---------------------------------------------------------------------------
5
-
6
-
7
- (define i 0)
8
- (define offset 0)
9
- (define li (list 1 2 3 4 5 6))
10
-
11
- (define each
12
- (function (lis)
13
- (if (= (+ (length lis) offset) i)
14
- (display "")
15
- (do
16
- (write (listFirst lis))
17
- (define lis (shift lis))
18
- (define i (+ i 1))
19
- (define offset (+ offset 1))
20
- (each lis)))))
21
-
22
- (each li)
@@ -1,44 +0,0 @@
1
- (define i 0)
2
- (define p 0)
3
- (define c 1)
4
-
5
- (define test
6
- (function (x)
7
- (if(= i x)
8
- (do
9
- (define i 0)
10
- (define p 0)
11
- (define c 1)
12
- )
13
- (do
14
- (define np c)
15
- (define nc (+ p c))
16
- (define p np)
17
- (define c nc)
18
- (write c)
19
- (define i (+ i 1))
20
- (test x)
21
- )
22
- )
23
- )
24
- )
25
-
26
- (define run
27
- (function ()
28
- (do
29
- (write "How many Fibbonocci numbers would you like me to calculate?")
30
- (display ">> ")
31
- (define num (input))
32
- (if (= num "exit")
33
- (write "")
34
- (do
35
- (define num (->num num))
36
- (test num)
37
- (run)
38
- )
39
- )
40
- )
41
- )
42
- )
43
-
44
- (run)
@@ -1,7 +0,0 @@
1
- (define listFirst
2
- (native_function "
3
- Proc.new do |argument, interpreter|
4
- list = interpreter.evaluate(argument[0])
5
- list[0]
6
- end
7
- "))