mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Оператор println
This commit is contained in:
parent
556a0be4c2
commit
1f6a17a024
79
program.own
79
program.own
@ -3,32 +3,30 @@ use "std"
|
||||
word = 2 + 2
|
||||
word2 = PI + word
|
||||
str = "a" * 5 + "ba" * 7 + "\n"
|
||||
print str * "3" * 2
|
||||
print "word = " + word + "\n"
|
||||
print "word2 = " + word2 + "\n"
|
||||
print "1" > "abc"
|
||||
print "\n"
|
||||
if (1 <= 2) print "1 = 1"
|
||||
else print "1 != 1"
|
||||
print "\n"
|
||||
println str * "3" * 2
|
||||
println "word = " + word
|
||||
println "word2 = " + word2
|
||||
println "1" > "abc"
|
||||
if (1 <= 2) println "1 = 1"
|
||||
else println "1 != 1"
|
||||
if (40 < 50 || 50 > 60) {
|
||||
print "true1\n"
|
||||
print "true2\n"
|
||||
println "true1"
|
||||
println "true2"
|
||||
i = 0
|
||||
print "do while"
|
||||
println "do while"
|
||||
do {
|
||||
print "i = " + i + "\n"
|
||||
println "i = " + i
|
||||
i = i + 1
|
||||
} while (i < 10)
|
||||
i = 0
|
||||
print "while"
|
||||
println "while"
|
||||
while (i < 10) {
|
||||
print "i = " + i + sin(i) + "\n"
|
||||
println "i = " + i + sin(i)
|
||||
i = i + 1
|
||||
}
|
||||
print "for"
|
||||
println "for"
|
||||
for i = 0, i < 10, i = i + 1 {
|
||||
print "i = " + i + "\n"
|
||||
println "i = " + i
|
||||
}
|
||||
}
|
||||
else print "false"
|
||||
@ -53,16 +51,16 @@ print sum(10, 15)
|
||||
print a + "\n"
|
||||
|
||||
arr = [1, "text", sum(10, 15), [], ["text", [90, [7 + 6, [50]]]]]
|
||||
print arr + "\n"
|
||||
println arr + "\n"
|
||||
arr[0] = arr[0] + 1000 + arr[2]
|
||||
print arr + "\n"
|
||||
print arr[4][1] + "\n"
|
||||
arr[4][1] = "text"
|
||||
print arr[4][1] + "\n"
|
||||
println arr[4][1]
|
||||
|
||||
print "\n\n"
|
||||
array = newarray(2, 2, 2, 2)
|
||||
print array
|
||||
println array
|
||||
|
||||
add = 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
|
||||
cube = def(x) = x*mul(x, x)
|
||||
print "\n\n"
|
||||
print mul(8, 5)
|
||||
print "\n"
|
||||
print cube(2)
|
||||
println mul(8, 5)
|
||||
println cube(2)
|
||||
|
||||
functions = [add, sub, mul, div]
|
||||
def function(f, a, b) = f(a, b)
|
||||
for i = 0, i < 4, i = i + 1 {
|
||||
print "\n"
|
||||
print functions[i]
|
||||
print "\n"
|
||||
print function(functions[i], 6, 3)
|
||||
println functions[i]
|
||||
println function(functions[i], 6, 3)
|
||||
}
|
||||
|
||||
// map
|
||||
map = {"+" : add, "-" : sub. "*" : mul, "/" : div}
|
||||
map["%"] = def(x,y) = x % y
|
||||
map["pow"] = def(x,y) = pow(x, y)
|
||||
//print map["+"]
|
||||
print "\n"
|
||||
print function(map["+"], 4, 5)
|
||||
print "\n"
|
||||
println map["+"]
|
||||
println function(map["+"], 4, 5)
|
||||
foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5)))
|
||||
foreach(arr, ::echo)
|
||||
|
||||
arr1 = [1,2,3]
|
||||
arr1 = arr1 :: 4
|
||||
arr2 = [5,6,7]
|
||||
print arr1
|
||||
print "\n"
|
||||
print arr1 << arr2
|
||||
println arr1
|
||||
println arr1 << arr2
|
||||
|
||||
for op, func : map {
|
||||
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 + ", "
|
||||
|
||||
use "types"
|
||||
print "\n"
|
||||
print typeof(1)
|
||||
print "\n"
|
||||
print typeof("1")
|
||||
print "\n"
|
||||
print typeof(arr1)
|
||||
print "\n"
|
||||
print typeof({})
|
||||
print "\n"
|
||||
print typeof(add)
|
||||
println typeof(1)
|
||||
println typeof("1")
|
||||
println typeof(arr1)
|
||||
println typeof({})
|
||||
println typeof(add)
|
||||
|
||||
print "\n"
|
||||
print typeof(number("1"))
|
||||
print "\n"
|
||||
print typeof(string(1))
|
||||
println typeof(number("1"))
|
||||
println typeof(string(1))
|
@ -31,7 +31,7 @@ public final class Main {
|
||||
final Statement program = new Parser(tokens).parse();
|
||||
System.out.println(program.toString());
|
||||
program.accept(new FunctionAdder());
|
||||
program.accept(new VariablePrinter());
|
||||
// program.accept(new VariablePrinter());
|
||||
program.accept(new AssignValidator());
|
||||
program.execute();
|
||||
}
|
||||
|
@ -161,6 +161,7 @@ public final class Lexer {
|
||||
final String word = buffer.toString();
|
||||
switch (word) {
|
||||
case "print": addToken(TokenType.PRINT); break;
|
||||
case "println": addToken(TokenType.PRINTLN); break;
|
||||
case "if": addToken(TokenType.IF); break;
|
||||
case "else": addToken(TokenType.ELSE); break;
|
||||
case "while": addToken(TokenType.WHILE); break;
|
||||
|
@ -51,6 +51,9 @@ public final class Parser {
|
||||
if (match(TokenType.PRINT)) {
|
||||
return new PrintStatement(expression());
|
||||
}
|
||||
if (match(TokenType.PRINTLN)) {
|
||||
return new PrintlnStatement(expression());
|
||||
}
|
||||
if (match(TokenType.IF)) {
|
||||
return ifElse();
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ public enum TokenType {
|
||||
|
||||
// keyword
|
||||
PRINT,
|
||||
PRINTLN,
|
||||
IF,
|
||||
ELSE,
|
||||
WHILE,
|
||||
|
29
src/com/annimon/ownlang/parser/ast/PrintlnStatement.java
Normal file
29
src/com/annimon/ownlang/parser/ast/PrintlnStatement.java
Normal 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;
|
||||
}
|
||||
}
|
@ -26,6 +26,7 @@ public interface Visitor {
|
||||
void visit(IfStatement s);
|
||||
void visit(MapExpression s);
|
||||
void visit(PrintStatement s);
|
||||
void visit(PrintlnStatement s);
|
||||
void visit(ReturnStatement s);
|
||||
void visit(TernaryExpression s);
|
||||
void visit(UnaryExpression s);
|
||||
|
@ -131,6 +131,11 @@ public abstract class AbstractVisitor implements Visitor {
|
||||
s.expression.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(PrintlnStatement s) {
|
||||
s.expression.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ReturnStatement s) {
|
||||
s.expression.accept(this);
|
||||
|
Loading…
Reference in New Issue
Block a user