Skip to content

Commit 78d6476

Browse files
committed
Initial implementation and documentation
1 parent 144992a commit 78d6476

3 files changed

Lines changed: 310 additions & 1 deletion

File tree

README.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,126 @@
1-
# Emo
1+
# Emo
2+
Programming is a very emotional process. Therefore there is a need for a language that allows us to express our emotions _within_ the code. Emo fills this need.
3+
4+
# Usage
5+
`python emo_interpreter.py example.emo`
6+
7+
# Example
8+
```Emo
9+
:X This is a comment
10+
:X Best practice is to put two spaces between the comment
11+
:X indicator and the content of a comment
12+
:X This is the same with all operators
13+
14+
15+
:X Strings, ints, and floats are what you'd expect
16+
17+
"Hello" :X Strings
18+
-25 :X Ints
19+
3.14 :X Floats
20+
21+
22+
:X Print statement is tongue-sticking-out-face
23+
24+
:P "This prints with a newline"
25+
:p "This prints without a newline"
26+
27+
28+
:X Arithmetic Operators
29+
30+
:P 4 +_+ 3 :X Addition
31+
:P 4 -_- 3 :X Subtraction
32+
:P 4 *_* 3 :X Multiplication
33+
:P 4 ^_^ 3 :X Exponentiation
34+
:P 4 %_% 3 :X Modulo
35+
:P 4 :\/ 3 :X Derpy Division (uses integer division)
36+
37+
38+
:X Comparison operators
39+
40+
:P 4 <_< 3 :X Less than
41+
:P 4 >_> 3 :X Greater than
42+
:P 4 =_= 3 :X Equality
43+
:P 4 >_< 3 :X Inequality
44+
45+
46+
:X Variable names consist of eyes, a nose, and a mouth consisting of one letter
47+
:X Assignment operator is beaver face
48+
49+
:-y := 5
50+
:P :-y :X -> 5
51+
52+
53+
:X Arrays are declared with square-smiley delimiters and
54+
:X crying-small-face separators
55+
:X They are accessed with square-smileys
56+
57+
:-o := [: 'first' ,_, 'second' ,_, 'third' :]
58+
59+
:P :-o [: 1 :] :X -> 'second'
60+
:-o [: 1 :] := 'newval'
61+
:P :-o :X -> ['first', 'newval', 'third']
62+
63+
:X Associative arrays are declared with curly-smiley delimiters,
64+
:X crying-small-face separators, and use very-happy-smileys to
65+
:X relate keys and values. They are accessed with square-smileys
66+
:-n := {:
67+
'key1' => 'val1' ,_,
68+
'key2' => 'val2' ,_,
69+
'key3' => 'val2'
70+
:}
71+
72+
:P :-n [: 'key1' :] :X -> 'val1'
73+
74+
:X Control statements
75+
:X Indentation is significant
76+
77+
(?_?) :-y <_< 0 :X If
78+
:P "Negative"
79+
(!_?) :-y >_> 0 :X Else if
80+
:P "Positive"
81+
(!_!) :X Else
82+
:P "Zero"
83+
84+
85+
(@_@) :-o :X For loop
86+
:P :-i :X :-i is the implicit index variable
87+
88+
89+
90+
:X Exceptions
91+
92+
( -_-) :X Try
93+
:P "Trying operation"
94+
\(>o<)/ "Badness!" :X Throw exception
95+
( O_O) :X Catch
96+
:P "Caught an exception"
97+
(;-_-) :X Finally
98+
:P "Cleaning up"
99+
100+
:X Functions are declared with a kissy-face,
101+
:X the name of the function, and then a round-smiley
102+
:X list of arguments
103+
:X The 'for-you' gesture returns from the function
104+
105+
:X Add function
106+
( ')3 :-a (: :-x ,_, :-y :)
107+
(>")> :-x +_+ :-y
108+
109+
:p "Sum of 5 and 6: "
110+
:P :-a (: 5 ,_, 6 :)
111+
112+
113+
:X Boolean literals are happy face and sad face
114+
:-) :X true
115+
:-( :X false
116+
117+
:X Boolean operators
118+
:P :-) :& :-( :X and
119+
:P :-) :| :-( :X or
120+
:P :! :-( :X not
121+
122+
:X Generate a random value between 0.0 and 1.0
123+
:X with the waffle operator
124+
:P (#)
125+
126+
```

emo_interpreter.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/python -u
2+
3+
import re, sys
4+
from collections import OrderedDict
5+
6+
preamble = '''
7+
import random
8+
import sys
9+
10+
'''
11+
12+
faceRepMap = OrderedDict({
13+
':P' : 'print ',
14+
':p' : 'sys.stdout.write(str(\\1))',
15+
'(?_?)' : 'if \\1:',
16+
'(!_?)' : 'elif \\1:',
17+
'(!_!)' : 'else:',
18+
'._.' : '.',
19+
',_,' : ',',
20+
'( -_-)' : 'try:',
21+
'( O_O)' : 'except:',
22+
'(;-_-)' : 'finally:',
23+
'( \')3' : 'def \\1 :', # I know, these don't
24+
'(@_@)' : 'for i in \\1 :', # make any sense
25+
'*_*' : '*',
26+
'+_+' : '+',
27+
'%_%' : '%',
28+
'^_^' : '**',
29+
':\\/' : '/',
30+
'>_>' : '>',
31+
'<_<' : '<',
32+
'=_=' : '==',
33+
'>_<' : '!=',
34+
'(:' : '(',
35+
':)' : ')',
36+
'[:' : '[',
37+
':]' : ']',
38+
'{:' : '{',
39+
':}' : '}',
40+
':-)' : 'True',
41+
':-(' : 'False',
42+
':|' : 'or',
43+
':&' : 'and',
44+
':!' : 'not',
45+
'=>' : ':',
46+
'(#)' : 'random.random()',
47+
':=' : '=',
48+
'(>")>' : 'return ',
49+
'(^-^)/' : 'exit(\\1)',
50+
'\\(>o<)/' : 'raise Exception(\\1)'
51+
})
52+
53+
faceRepMap['-_-'] = '-' # ensure this will be done last
54+
55+
def run(fileName='example.emo'):
56+
raw = preamble + open(fileName).read()
57+
raw = re.sub(':X.*', '', raw) # Strip comments
58+
raw = re.sub(':-([A-z]+)', '\\1', raw) # Substitute identifiers
59+
for face, repl in faceRepMap.items(): # Substitute operators, etc.
60+
if '\\1' in repl:
61+
regex = re.escape(face) + '(.*)'
62+
raw = re.sub(regex, repl, raw)
63+
else:
64+
raw = raw.replace(face, repl)
65+
exec raw
66+
67+
if len(sys.argv) > 1:
68+
run(sys.argv[1])

example.emo

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
:X This is a comment
2+
:X Best practice is to put two spaces between the comment
3+
:X indicator and the content of a comment
4+
:X This is the same with all operators
5+
6+
7+
:X Strings, ints, and floats are what you'd expect
8+
9+
"Hello" :X Strings
10+
-25 :X Ints
11+
3.14 :X Floats
12+
13+
14+
:X Print statement is tongue-sticking-out-face
15+
16+
:P "This prints with a newline"
17+
:p "This prints without a newline"
18+
19+
20+
:X Arithmetic Operators
21+
22+
:P 4 +_+ 3 :X Addition
23+
:P 4 -_- 3 :X Subtraction
24+
:P 4 *_* 3 :X Multiplication
25+
:P 4 ^_^ 3 :X Exponentiation
26+
:P 4 %_% 3 :X Modulo
27+
:P 4 :\/ 3 :X Derpy Division (uses integer division)
28+
29+
30+
:X Comparison operators
31+
32+
:P 4 <_< 3 :X Less than
33+
:P 4 >_> 3 :X Greater than
34+
:P 4 =_= 3 :X Equality
35+
:P 4 >_< 3 :X Inequality
36+
37+
38+
:X Variable names consist of eyes, a nose, and a mouth consisting of one letter
39+
:X Assignment operator is beaver face
40+
41+
:-y := 5
42+
:P :-y :X -> 5
43+
44+
45+
:X Arrays are declared with square-smiley delimiters and
46+
:X crying-small-face separators
47+
:X They are accessed with square-smileys
48+
49+
:-o := [: 'first' ,_, 'second' ,_, 'third' :]
50+
51+
:P :-o [: 1 :] :X -> 'second'
52+
:-o [: 1 :] := 'newval'
53+
:P :-o :X -> ['first', 'newval', 'third']
54+
55+
:X Associative arrays are declared with curly-smiley delimiters,
56+
:X crying-small-face separators, and use very-happy-smileys to
57+
:X relate keys and values. They are accessed with square-smileys
58+
:-n := {:
59+
'key1' => 'val1' ,_,
60+
'key2' => 'val2' ,_,
61+
'key3' => 'val2'
62+
:}
63+
64+
:P :-n [: 'key1' :] :X -> 'val1'
65+
66+
:X Control statements
67+
:X Indentation is significant
68+
69+
(?_?) :-y <_< 0 :X If
70+
:P "Negative"
71+
(!_?) :-y >_> 0 :X Else if
72+
:P "Positive"
73+
(!_!) :X Else
74+
:P "Zero"
75+
76+
77+
(@_@) :-o :X For loop
78+
:P :-i :X :-i is the implicit index variable
79+
80+
81+
82+
:X Exceptions
83+
84+
( -_-) :X Try
85+
:P "Trying operation"
86+
\(>o<)/ "Badness!" :X Throw exception
87+
( O_O) :X Catch
88+
:P "Caught an exception"
89+
(;-_-) :X Finally
90+
:P "Cleaning up"
91+
92+
:X Functions are declared with a kissy-face,
93+
:X the name of the function, and then a round-smiley
94+
:X list of arguments
95+
:X The 'for-you' gesture returns from the function
96+
97+
:X Add function
98+
( ')3 :-a (: :-x ,_, :-y :)
99+
(>")> :-x +_+ :-y
100+
101+
:p "Sum of 5 and 6: "
102+
:P :-a (: 5 ,_, 6 :)
103+
104+
105+
:X Boolean literals are happy face and sad face
106+
:-) :X true
107+
:-( :X false
108+
109+
:X Boolean operators
110+
:P :-) :& :-( :X and
111+
:P :-) :| :-( :X or
112+
:P :! :-( :X not
113+
114+
:X Generate a random value between 0.0 and 1.0
115+
:X with the waffle operator
116+
:P (#)

0 commit comments

Comments
 (0)