Skip to content

Commit

Permalink
Add function
Browse files Browse the repository at this point in the history
  • Loading branch information
TenFont committed Nov 6, 2023
1 parent fe44b8c commit c0521f7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
import dev.tenfont.cpmm.lang.components.Context;
import dev.tenfont.cpmm.lang.components.Statement;
import dev.tenfont.cpmm.lang.components.TokenType;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class FunctionDeclarationStatement extends Statement {
private final BlockStatement body;
private String identifier;

@Override
public void execute(Interpreter interpreter) {
System.out.println("Function declaration statement executing test.");
}

@Override
public boolean init(Parser parser, Context context) {
parser.eat(TokenType.FUNCTION);
identifier = parser.eat(TokenType.IDENTIFIER, String.class);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
import dev.tenfont.cpmm.lang.components.Expression;
import dev.tenfont.cpmm.lang.components.Statement;
import dev.tenfont.cpmm.lang.components.TokenType;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class IfStatement extends Statement {
private final BlockStatement blockStatement;
private final BlockStatement body;
private Expression test;

public IfStatement(BlockStatement blockStatement) {
this.blockStatement = blockStatement;
}

@Override
public void execute(Interpreter interpreter) {
var value = test.get(interpreter.getCurrentContext());
if (value != null && (boolean) value) blockStatement.execute(interpreter);
if (value != null && (boolean) value) body.execute(interpreter);
}

@Override
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/dev/tenfont/cpmm/lang/FunctionRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.tenfont.cpmm.lang;

import dev.tenfont.cpmm.lang.components.Function;

import java.util.HashMap;
import java.util.Map;

public class FunctionRegistry {
private final Map<String, Function> map = new HashMap<>();

public boolean declareFunction(String name, Function function) {
if (map.containsKey(name))
return false;
map.put(name, function);
return true;
}

public boolean functionExists(String name) {
return map.containsKey(name);
}
}
4 changes: 3 additions & 1 deletion src/main/java/dev/tenfont/cpmm/lang/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public Statement parseStatement(Context context) {
return this.parseBlockStatement(context);
}
case REVERSE -> statement = new ReverseStatement();
case FUNCTION -> statement = new FunctionDeclarationStatement();
case VARIABLE_DECLARATION -> statement = new VariableDeclarationStatement();
default -> statement = new ExpressionStatement();
}
Expand Down Expand Up @@ -123,6 +122,9 @@ public Statement parseBlockStatement(Context context) {
statement = new IfStatement(block);
statement.init(this, context);
eat(TokenType.END_STATEMENT);
} case FUNCTION -> {
statement.init(this, context);
eat(TokenType.END_STATEMENT);
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/dev/tenfont/cpmm/lang/components/Context.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.tenfont.cpmm.lang.components;

import dev.tenfont.cpmm.lang.FunctionRegistry;
import dev.tenfont.cpmm.lang.VariableMap;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -11,21 +12,23 @@
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class Context {
// TODO include optional script object, variable map and function registry
// TODO include optional script object
private final @Nullable Context parentContext;
private final VariableMap variableMap;
private final FunctionRegistry functionRegistry;

public Context() {
this(new VariableMap());
}

public Context(VariableMap variableMap) {
this.parentContext = null;
this.functionRegistry = new FunctionRegistry();
this.variableMap = variableMap;
}

public Context enterScope() {
return new Context(this, new VariableMap(variableMap));
return new Context(this, new VariableMap(variableMap), functionRegistry);
}

public Context exitScope() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/dev/tenfont/cpmm/lang/components/Function.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.tenfont.cpmm.lang.components;

public interface Function {
void execute(Object[] arguments);
}
16 changes: 16 additions & 0 deletions src/main/java/dev/tenfont/cpmm/lang/components/ScriptFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.tenfont.cpmm.lang.components;

import dev.tenfont.cpmm.lang.Interpreter;
import lombok.RequiredArgsConstructor;

import java.util.LinkedList;

@RequiredArgsConstructor
public class ScriptFunction implements Function {
private final LinkedList<Statement> statements;

@Override
public void execute(Object[] arguments) {
new Interpreter(statements).execute();
}
}

0 comments on commit c0521f7

Please sign in to comment.