Оператор println

This commit is contained in:
Victor 2016-01-09 14:11:15 +02:00
parent 556a0be4c2
commit 1f6a17a024
8 changed files with 73 additions and 48 deletions

View File

@ -3,32 +3,30 @@ use "std"
word = 2 + 2 word = 2 + 2
word2 = PI + word word2 = PI + word
str = "a" * 5 + "ba" * 7 + "\n" str = "a" * 5 + "ba" * 7 + "\n"
print str * "3" * 2 println str * "3" * 2
print "word = " + word + "\n" println "word = " + word
print "word2 = " + word2 + "\n" println "word2 = " + word2
print "1" > "abc" println "1" > "abc"
print "\n" if (1 <= 2) println "1 = 1"
if (1 <= 2) print "1 = 1" else println "1 != 1"
else print "1 != 1"
print "\n"
if (40 < 50 || 50 > 60) { if (40 < 50 || 50 > 60) {
print "true1\n" println "true1"
print "true2\n" println "true2"
i = 0 i = 0
print "do while" println "do while"
do { do {
print "i = " + i + "\n" println "i = " + i
i = i + 1 i = i + 1
} while (i < 10) } while (i < 10)
i = 0 i = 0
print "while" println "while"
while (i < 10) { while (i < 10) {
print "i = " + i + sin(i) + "\n" println "i = " + i + sin(i)
i = i + 1 i = i + 1
} }
print "for" println "for"
for i = 0, i < 10, i = i + 1 { for i = 0, i < 10, i = i + 1 {
print "i = " + i + "\n" println "i = " + i
} }
} }
else print "false" else print "false"
@ -53,16 +51,16 @@ print sum(10, 15)
print a + "\n" print a + "\n"
arr = [1, "text", sum(10, 15), [], ["text", [90, [7 + 6, [50]]]]] arr = [1, "text", sum(10, 15), [], ["text", [90, [7 + 6, [50]]]]]
print arr + "\n" println arr + "\n"
arr[0] = arr[0] + 1000 + arr[2] arr[0] = arr[0] + 1000 + arr[2]
print arr + "\n" print arr + "\n"
print arr[4][1] + "\n" print arr[4][1] + "\n"
arr[4][1] = "text" arr[4][1] = "text"
print arr[4][1] + "\n" println arr[4][1]
print "\n\n" print "\n\n"
array = newarray(2, 2, 2, 2) array = newarray(2, 2, 2, 2)
print array println array
add = def(a,b) = a+b add = def(a,b) = a+b
sub = def(a,b) = a-b sub = def(a,b) = a-b
@ -70,36 +68,30 @@ mul = def(a,b) = a*b
div = def(a,b) = a/b div = def(a,b) = a/b
cube = def(x) = x*mul(x, x) cube = def(x) = x*mul(x, x)
print "\n\n" print "\n\n"
print mul(8, 5) println mul(8, 5)
print "\n" println cube(2)
print cube(2)
functions = [add, sub, mul, div] functions = [add, sub, mul, div]
def function(f, a, b) = f(a, b) def function(f, a, b) = f(a, b)
for i = 0, i < 4, i = i + 1 { for i = 0, i < 4, i = i + 1 {
print "\n" println functions[i]
print functions[i] println function(functions[i], 6, 3)
print "\n"
print function(functions[i], 6, 3)
} }
// map // map
map = {"+" : add, "-" : sub. "*" : mul, "/" : div} map = {"+" : add, "-" : sub. "*" : mul, "/" : div}
map["%"] = def(x,y) = x % y map["%"] = def(x,y) = x % y
map["pow"] = def(x,y) = pow(x, y) map["pow"] = def(x,y) = pow(x, y)
//print map["+"] println map["+"]
print "\n" println function(map["+"], 4, 5)
print function(map["+"], 4, 5)
print "\n"
foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5))) foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5)))
foreach(arr, ::echo) foreach(arr, ::echo)
arr1 = [1,2,3] arr1 = [1,2,3]
arr1 = arr1 :: 4 arr1 = arr1 :: 4
arr2 = [5,6,7] arr2 = [5,6,7]
print arr1 println arr1
print "\n" println arr1 << arr2
print arr1 << arr2
for op, func : map { for op, func : map {
echo (4, op, 5, "=", func(4,5)) echo (4, op, 5, "=", func(4,5))
@ -112,18 +104,11 @@ print "\n"
for v : [1,2,3,4,5,6,7,8,9] print "" + v + ", " for v : [1,2,3,4,5,6,7,8,9] print "" + v + ", "
use "types" use "types"
print "\n" println typeof(1)
print typeof(1) println typeof("1")
print "\n" println typeof(arr1)
print typeof("1") println typeof({})
print "\n" println typeof(add)
print typeof(arr1)
print "\n"
print typeof({})
print "\n"
print typeof(add)
print "\n" println typeof(number("1"))
print typeof(number("1")) println typeof(string(1))
print "\n"
print typeof(string(1))

View File

@ -31,7 +31,7 @@ public final class Main {
final Statement program = new Parser(tokens).parse(); final Statement program = new Parser(tokens).parse();
System.out.println(program.toString()); System.out.println(program.toString());
program.accept(new FunctionAdder()); program.accept(new FunctionAdder());
program.accept(new VariablePrinter()); // program.accept(new VariablePrinter());
program.accept(new AssignValidator()); program.accept(new AssignValidator());
program.execute(); program.execute();
} }

View File

@ -161,6 +161,7 @@ public final class Lexer {
final String word = buffer.toString(); final String word = buffer.toString();
switch (word) { switch (word) {
case "print": addToken(TokenType.PRINT); break; case "print": addToken(TokenType.PRINT); break;
case "println": addToken(TokenType.PRINTLN); break;
case "if": addToken(TokenType.IF); break; case "if": addToken(TokenType.IF); break;
case "else": addToken(TokenType.ELSE); break; case "else": addToken(TokenType.ELSE); break;
case "while": addToken(TokenType.WHILE); break; case "while": addToken(TokenType.WHILE); break;

View File

@ -51,6 +51,9 @@ public final class Parser {
if (match(TokenType.PRINT)) { if (match(TokenType.PRINT)) {
return new PrintStatement(expression()); return new PrintStatement(expression());
} }
if (match(TokenType.PRINTLN)) {
return new PrintlnStatement(expression());
}
if (match(TokenType.IF)) { if (match(TokenType.IF)) {
return ifElse(); return ifElse();
} }

View File

@ -13,6 +13,7 @@ public enum TokenType {
// keyword // keyword
PRINT, PRINT,
PRINTLN,
IF, IF,
ELSE, ELSE,
WHILE, WHILE,

View File

@ -0,0 +1,29 @@
package com.annimon.ownlang.parser.ast;
/**
*
* @author aNNiMON
*/
public final class PrintlnStatement implements Statement {
public final Expression expression;
public PrintlnStatement(Expression expression) {
this.expression = expression;
}
@Override
public void execute() {
System.out.println(expression.eval());
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
@Override
public String toString() {
return "println " + expression;
}
}

View File

@ -26,6 +26,7 @@ public interface Visitor {
void visit(IfStatement s); void visit(IfStatement s);
void visit(MapExpression s); void visit(MapExpression s);
void visit(PrintStatement s); void visit(PrintStatement s);
void visit(PrintlnStatement s);
void visit(ReturnStatement s); void visit(ReturnStatement s);
void visit(TernaryExpression s); void visit(TernaryExpression s);
void visit(UnaryExpression s); void visit(UnaryExpression s);

View File

@ -131,6 +131,11 @@ public abstract class AbstractVisitor implements Visitor {
s.expression.accept(this); s.expression.accept(this);
} }
@Override
public void visit(PrintlnStatement s) {
s.expression.accept(this);
}
@Override @Override
public void visit(ReturnStatement s) { public void visit(ReturnStatement s) {
s.expression.accept(this); s.expression.accept(this);