Skip to content

Commit 1f6a17a

Browse files
committed
Оператор println
1 parent 556a0be commit 1f6a17a

File tree

8 files changed

+73
-48
lines changed

8 files changed

+73
-48
lines changed

program.own

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,30 @@ use "std"
33
word = 2 + 2
44
word2 = PI + word
55
str = "a" * 5 + "ba" * 7 + "\n"
6-
print str * "3" * 2
7-
print "word = " + word + "\n"
8-
print "word2 = " + word2 + "\n"
9-
print "1" > "abc"
10-
print "\n"
11-
if (1 <= 2) print "1 = 1"
12-
else print "1 != 1"
13-
print "\n"
6+
println str * "3" * 2
7+
println "word = " + word
8+
println "word2 = " + word2
9+
println "1" > "abc"
10+
if (1 <= 2) println "1 = 1"
11+
else println "1 != 1"
1412
if (40 < 50 || 50 > 60) {
15-
print "true1\n"
16-
print "true2\n"
13+
println "true1"
14+
println "true2"
1715
i = 0
18-
print "do while"
16+
println "do while"
1917
do {
20-
print "i = " + i + "\n"
18+
println "i = " + i
2119
i = i + 1
2220
} while (i < 10)
2321
i = 0
24-
print "while"
22+
println "while"
2523
while (i < 10) {
26-
print "i = " + i + sin(i) + "\n"
24+
println "i = " + i + sin(i)
2725
i = i + 1
2826
}
29-
print "for"
27+
println "for"
3028
for i = 0, i < 10, i = i + 1 {
31-
print "i = " + i + "\n"
29+
println "i = " + i
3230
}
3331
}
3432
else print "false"
@@ -53,53 +51,47 @@ print sum(10, 15)
5351
print a + "\n"
5452

5553
arr = [1, "text", sum(10, 15), [], ["text", [90, [7 + 6, [50]]]]]
56-
print arr + "\n"
54+
println arr + "\n"
5755
arr[0] = arr[0] + 1000 + arr[2]
5856
print arr + "\n"
5957
print arr[4][1] + "\n"
6058
arr[4][1] = "text"
61-
print arr[4][1] + "\n"
59+
println arr[4][1]
6260

6361
print "\n\n"
6462
array = newarray(2, 2, 2, 2)
65-
print array
63+
println array
6664

6765
add = def(a,b) = a+b
6866
sub = def(a,b) = a-b
6967
mul = def(a,b) = a*b
7068
div = def(a,b) = a/b
7169
cube = def(x) = x*mul(x, x)
7270
print "\n\n"
73-
print mul(8, 5)
74-
print "\n"
75-
print cube(2)
71+
println mul(8, 5)
72+
println cube(2)
7673

7774
functions = [add, sub, mul, div]
7875
def function(f, a, b) = f(a, b)
7976
for i = 0, i < 4, i = i + 1 {
80-
print "\n"
81-
print functions[i]
82-
print "\n"
83-
print function(functions[i], 6, 3)
77+
println functions[i]
78+
println function(functions[i], 6, 3)
8479
}
8580

8681
// map
8782
map = {"+" : add, "-" : sub. "*" : mul, "/" : div}
8883
map["%"] = def(x,y) = x % y
8984
map["pow"] = def(x,y) = pow(x, y)
90-
//print map["+"]
91-
print "\n"
92-
print function(map["+"], 4, 5)
93-
print "\n"
85+
println map["+"]
86+
println function(map["+"], 4, 5)
9487
foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5)))
9588
foreach(arr, ::echo)
9689

9790
arr1 = [1,2,3]
9891
arr1 = arr1 :: 4
9992
arr2 = [5,6,7]
100-
print arr1
101-
print "\n"
102-
print arr1 << arr2
93+
println arr1
94+
println arr1 << arr2
10395

10496
for op, func : map {
10597
echo (4, op, 5, "=", func(4,5))
@@ -112,18 +104,11 @@ print "\n"
112104
for v : [1,2,3,4,5,6,7,8,9] print "" + v + ", "
113105

114106
use "types"
115-
print "\n"
116-
print typeof(1)
117-
print "\n"
118-
print typeof("1")
119-
print "\n"
120-
print typeof(arr1)
121-
print "\n"
122-
print typeof({})
123-
print "\n"
124-
print typeof(add)
107+
println typeof(1)
108+
println typeof("1")
109+
println typeof(arr1)
110+
println typeof({})
111+
println typeof(add)
125112

126-
print "\n"
127-
print typeof(number("1"))
128-
print "\n"
129-
print typeof(string(1))
113+
println typeof(number("1"))
114+
println typeof(string(1))

src/com/annimon/ownlang/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static void main(String[] args) throws IOException {
3131
final Statement program = new Parser(tokens).parse();
3232
System.out.println(program.toString());
3333
program.accept(new FunctionAdder());
34-
program.accept(new VariablePrinter());
34+
// program.accept(new VariablePrinter());
3535
program.accept(new AssignValidator());
3636
program.execute();
3737
}

src/com/annimon/ownlang/parser/Lexer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ private void tokenizeWord() {
161161
final String word = buffer.toString();
162162
switch (word) {
163163
case "print": addToken(TokenType.PRINT); break;
164+
case "println": addToken(TokenType.PRINTLN); break;
164165
case "if": addToken(TokenType.IF); break;
165166
case "else": addToken(TokenType.ELSE); break;
166167
case "while": addToken(TokenType.WHILE); break;

src/com/annimon/ownlang/parser/Parser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ private Statement statement() {
5151
if (match(TokenType.PRINT)) {
5252
return new PrintStatement(expression());
5353
}
54+
if (match(TokenType.PRINTLN)) {
55+
return new PrintlnStatement(expression());
56+
}
5457
if (match(TokenType.IF)) {
5558
return ifElse();
5659
}

src/com/annimon/ownlang/parser/TokenType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum TokenType {
1313

1414
// keyword
1515
PRINT,
16+
PRINTLN,
1617
IF,
1718
ELSE,
1819
WHILE,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.annimon.ownlang.parser.ast;
2+
3+
/**
4+
*
5+
* @author aNNiMON
6+
*/
7+
public final class PrintlnStatement implements Statement {
8+
9+
public final Expression expression;
10+
11+
public PrintlnStatement(Expression expression) {
12+
this.expression = expression;
13+
}
14+
15+
@Override
16+
public void execute() {
17+
System.out.println(expression.eval());
18+
}
19+
20+
@Override
21+
public void accept(Visitor visitor) {
22+
visitor.visit(this);
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "println " + expression;
28+
}
29+
}

src/com/annimon/ownlang/parser/ast/Visitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public interface Visitor {
2626
void visit(IfStatement s);
2727
void visit(MapExpression s);
2828
void visit(PrintStatement s);
29+
void visit(PrintlnStatement s);
2930
void visit(ReturnStatement s);
3031
void visit(TernaryExpression s);
3132
void visit(UnaryExpression s);

src/com/annimon/ownlang/parser/visitors/AbstractVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public void visit(MapExpression s) {
130130
public void visit(PrintStatement s) {
131131
s.expression.accept(this);
132132
}
133+
134+
@Override
135+
public void visit(PrintlnStatement s) {
136+
s.expression.accept(this);
137+
}
133138

134139
@Override
135140
public void visit(ReturnStatement s) {

0 commit comments

Comments
 (0)