Skip to content

Commit b7c9aaf

Browse files
committed
"compilador unerg release 001"
1 parent 60fee1c commit b7c9aaf

18 files changed

+1139
-0
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Compilador basico para el lenguaje "UNERG"
2+
UNERG es un lenguaje de caracter muy basico creado para aprender a utilizar la libreria ply de python.
3+
4+
Puedes usarlo corriendo desde el terminal el siguiente comando:
5+
6+
% python basic.py hello.unerg
7+
HELLO WORLD
8+
%
9+
10+
O usarlo de forma interactiva (como python):
11+
12+
% python unerg.py
13+
[BASIC] 10 PRINT "HELLO WORLD"
14+
[BASIC] 20 END
15+
[BASIC] RUN
16+
HELLO WORLD
17+
[UNERG]
18+
19+
Esta es la definicion de los archivos utilizados:
20+
21+
unerg.py - Script de alto nivel que controla toda la app
22+
unergLex.py - tokenizador donde se definen los tokens
23+
unergParse.py - parser donde se definen las reglas para sintaxis
24+
unergInterp.py - interprete esto permite la ejecucion de los programas
25+
26+
debe usarse el sufijo ".unerg" para los programas, hay una lista de programas
27+
de ejemplo con la sintaxis definida por este pequeño compilador en la carpeta /examples
28+
29+
Sin mas que decir "Happy hacking"

examples/dim.unerg

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
5 DIM A(50,15)
2+
10 FOR I = 1 TO 50
3+
20 FOR J = 1 TO 15
4+
30 LET A(I,J) = I + J
5+
35 REM PRINT I,J, A(I,J)
6+
40 NEXT J
7+
50 NEXT I
8+
100 FOR I = 1 TO 50
9+
110 FOR J = 1 TO 15
10+
120 PRINT A(I,J),
11+
130 NEXT J
12+
140 PRINT
13+
150 NEXT I
14+
999 END

examples/func.unerg

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
10 DEF FDX(X) = 2*X
2+
20 FOR I = 0 TO 100
3+
30 PRINT FDX(I)
4+
40 NEXT I
5+
50 END

examples/gcd.unerg

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
10 PRINT "A","B","C","GCD"
2+
20 READ A,B,C
3+
30 LET X = A
4+
40 LET Y = B
5+
50 GOSUB 200
6+
60 LET X = G
7+
70 LET Y = C
8+
80 GOSUB 200
9+
90 PRINT A, B, C, G
10+
100 GOTO 20
11+
110 DATA 60, 90, 120
12+
120 DATA 38456, 64872, 98765
13+
130 DATA 32, 384, 72
14+
200 LET Q = INT(X/Y)
15+
210 LET R = X - Q*Y
16+
220 IF R = 0 THEN 300
17+
230 LET X = Y
18+
240 LET Y = R
19+
250 GOTO 200
20+
300 LET G = Y
21+
310 RETURN
22+
999 END

examples/gosub.unerg

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
100 LET X = 3
2+
110 GOSUB 400
3+
120 PRINT U, V, W
4+
200 LET X = 5
5+
210 GOSUB 400
6+
220 LET Z = U + 2*V + 3*W
7+
230 PRINT Z
8+
240 GOTO 999
9+
400 LET U = X*X
10+
410 LET V = X*X*X
11+
420 LET W = X*X*X*X + X*X*X + X*X + X
12+
430 RETURN
13+
999 END

examples/hello.unerg

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
5 REM HELLO WORLD PROGAM
2+
10 PRINT "HELLO WORLD"
3+
99 END
4+

examples/linear.unerg

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
1 REM ::: SOLVE A SYSTEM OF LINEAR EQUATIONS
2+
2 REM ::: A1*X1 + A2*X2 = B1
3+
3 REM ::: A3*X1 + A4*X2 = B2
4+
4 REM --------------------------------------
5+
10 READ A1, A2, A3, A4
6+
15 LET D = A1 * A4 - A3 * A2
7+
20 IF D = 0 THEN 65
8+
30 READ B1, B2
9+
37 LET X1 = (B1*A4 - B2*A2) / D
10+
42 LET X2 = (A1*B2 - A3*B1) / D
11+
55 PRINT X1, X2
12+
60 GOTO 30
13+
65 PRINT "NO UNIQUE SOLUTION"
14+
70 DATA 1, 2, 4
15+
80 DATA 2, -7, 5
16+
85 DATA 1, 3, 4, -7
17+
90 END

examples/maxsin.unerg

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
5 PRINT "X VALUE", "SINE", "RESOLUTION"
2+
10 READ D
3+
20 LET M = -1
4+
30 FOR X = 0 TO 3 STEP D
5+
40 IF SIN(X) <= M THEN 80
6+
50 LET X0 = X
7+
60 LET M = SIN(X)
8+
80 NEXT X
9+
85 PRINT X0, M, D
10+
90 GOTO 10
11+
100 DATA .1, .01, .001
12+
110 END

examples/powers.unerg

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
5 PRINT "THIS PROGRAM COMPUTES AND PRINTS THE NTH POWERS"
2+
6 PRINT "OF THE NUMBERS LESS THAN OR EQUAL TO N FOR VARIOUS"
3+
7 PRINT "N FROM 1 THROUGH 7"
4+
8 PRINT
5+
10 FOR N = 1 TO 7
6+
15 PRINT "N = "N
7+
20 FOR I = 1 TO N
8+
30 PRINT I^N,
9+
40 NEXT I
10+
50 PRINT
11+
60 PRINT
12+
70 NEXT N
13+
80 END

examples/rand.unerg

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
10 FOR I = 1 TO 20
2+
20 PRINT INT(10*RND(0))
3+
30 NEXT I
4+
40 END

examples/sales.unerg

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
10 FOR I = 1 TO 3
2+
20 READ P(I)
3+
30 NEXT I
4+
40 FOR I = 1 TO 3
5+
50 FOR J = 1 TO 5
6+
60 READ S(I,J)
7+
70 NEXT J
8+
80 NEXT I
9+
90 FOR J = 1 TO 5
10+
100 LET S = 0
11+
110 FOR I = 1 TO 3
12+
120 LET S = S + P(I) * S(I,J)
13+
130 NEXT I
14+
140 PRINT "TOTAL SALES FOR SALESMAN"J, "$"S
15+
150 NEXT J
16+
200 DATA 1.25, 4.30, 2.50
17+
210 DATA 40, 20, 37, 29, 42
18+
220 DATA 10, 16, 3, 21, 8
19+
230 DATA 35, 47, 29, 16, 33
20+
300 END

examples/sears.unerg

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
1 REM :: THIS PROGRAM COMPUTES HOW MANY TIMES YOU HAVE TO FOLD
2+
2 REM :: A PIECE OF PAPER SO THAT IT IS TALLER THAN THE
3+
3 REM :: SEARS TOWER.
4+
4 REM :: S = HEIGHT OF TOWER (METERS)
5+
5 REM :: T = THICKNESS OF PAPER (MILLIMETERS)
6+
10 LET S = 442
7+
20 LET T = 0.1
8+
30 REM CONVERT T TO METERS
9+
40 LET T = T * .001
10+
50 LET F = 1
11+
60 LET H = T
12+
100 IF H > S THEN 200
13+
120 LET H = 2 * H
14+
125 LET F = F + 1
15+
130 GOTO 100
16+
200 PRINT "NUMBER OF FOLDS ="F
17+
220 PRINT "FINAL HEIGHT ="H
18+
999 END

examples/sqrt1.unerg

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
10 LET X = 0
2+
20 LET X = X + 1
3+
30 PRINT X, SQR(X)
4+
40 IF X < 100 THEN 20
5+
50 END

examples/sqrt2.unerg

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
10 FOR X = 1 TO 100
2+
20 PRINT X, SQR(X)
3+
30 NEXT X
4+
40 END

unerg.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import sys
2+
sys.path.insert(0,"../..")
3+
4+
if sys.version_info[0] >= 3:
5+
raw_input = input
6+
7+
import unergLex
8+
import unergParse
9+
import unergInterp
10+
11+
# If a filename has been specified, we try to run it.
12+
# If a runtime error occurs, we bail out and enter
13+
# interactive mode below
14+
if len(sys.argv) == 2:
15+
data = open(sys.argv[1]).read()
16+
prog = basparse.parse(data)
17+
if not prog: raise SystemExit
18+
b = unergInterp.BasicInterpreter(prog)
19+
try:
20+
b.run()
21+
raise SystemExit
22+
except RuntimeError:
23+
pass
24+
25+
else:
26+
b = unergInterp.BasicInterpreter({})
27+
28+
# Interactive mode. This incrementally adds/deletes statements
29+
# from the program stored in the BasicInterpreter object. In
30+
# addition, special commands 'NEW','LIST',and 'RUN' are added.
31+
# Specifying a line number with no code deletes that line from
32+
# the program.
33+
34+
while 1:
35+
try:
36+
line = raw_input("[UNERG] ")
37+
except EOFError:
38+
raise SystemExit
39+
if not line: continue
40+
line += "\n"
41+
prog = unergParse.parse(line)
42+
if not prog: continue
43+
44+
keys = list(prog)
45+
if keys[0] > 0:
46+
b.add_statements(prog)
47+
else:
48+
stat = prog[keys[0]]
49+
if stat[0] == 'RUN':
50+
try:
51+
b.run()
52+
except RuntimeError:
53+
pass
54+
elif stat[0] == 'LIST':
55+
b.list()
56+
elif stat[0] == 'BLANK':
57+
b.del_line(stat[1])
58+
elif stat[0] == 'NEW':
59+
b.new()

0 commit comments

Comments
 (0)