mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-11 09:35:23 +00:00
Implement correct plugin loading and registration.
This commit is contained in:
@@ -3,14 +3,25 @@ package org.nwapw.abacus.tree;
|
||||
import org.nwapw.abacus.function.OperatorAssociativity;
|
||||
import org.nwapw.abacus.lexing.Lexer;
|
||||
import org.nwapw.abacus.lexing.pattern.Match;
|
||||
import org.nwapw.abacus.plugin.PluginManager;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* The builder responsible for turning strings into trees.
|
||||
*/
|
||||
public class TreeBuilder {
|
||||
|
||||
/**
|
||||
* The lexer used to get the input tokens.
|
||||
*/
|
||||
private Lexer<TokenType> lexer;
|
||||
/**
|
||||
* The map of operator precedences.
|
||||
*/
|
||||
private HashMap<String, Integer> precedenceMap;
|
||||
/**
|
||||
* The map of operator associativity.
|
||||
*/
|
||||
private HashMap<String, OperatorAssociativity> associativityMap;
|
||||
|
||||
/**
|
||||
@@ -18,6 +29,9 @@ public class TreeBuilder {
|
||||
*/
|
||||
protected static Comparator<TokenType> tokenSorter = Comparator.comparingInt(e -> e.priority);
|
||||
|
||||
/**
|
||||
* Creates a new TreeBuilder.
|
||||
*/
|
||||
public TreeBuilder(){
|
||||
lexer = new Lexer<TokenType>(){{
|
||||
register(",", TokenType.COMMA);
|
||||
@@ -29,6 +43,26 @@ public class TreeBuilder {
|
||||
associativityMap = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a function with the TreeBuilder.
|
||||
* @param function the function to register.
|
||||
*/
|
||||
public void registerFunction(String function){
|
||||
lexer.register(function, TokenType.FUNCTION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an operator with the TreeBuilder.
|
||||
* @param operator the operator to register.
|
||||
* @param precedence the precedence of the operator.
|
||||
* @param associativity the associativity of the operator.
|
||||
*/
|
||||
public void registerOperator(String operator, int precedence, OperatorAssociativity associativity){
|
||||
lexer.register(operator, TokenType.OP);
|
||||
precedenceMap.put(operator, precedence);
|
||||
associativityMap.put(operator, associativity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tokenizes a string, converting it into matches
|
||||
* @param string the string to tokenize.
|
||||
@@ -86,7 +120,9 @@ public class TreeBuilder {
|
||||
}
|
||||
}
|
||||
while(!tokenStack.empty()){
|
||||
if(!(tokenStack.peek().getType() == TokenType.OP)) return null;
|
||||
Match<TokenType> match = tokenStack.peek();
|
||||
TokenType matchType = match.getType();
|
||||
if(!(matchType == TokenType.OP || matchType == TokenType.FUNCTION)) return null;
|
||||
output.add(tokenStack.pop());
|
||||
}
|
||||
return output;
|
||||
|
||||
@@ -11,6 +11,12 @@ import java.util.*;
|
||||
*/
|
||||
public abstract class TreeNode {
|
||||
|
||||
/**
|
||||
* The function that reduces a tree to a single vale.
|
||||
* @param reducer the reducer used to reduce the tree.
|
||||
* @param <T> the type the reducer produces.
|
||||
* @return the result of the reduction, or null on error.
|
||||
*/
|
||||
public abstract <T> T reduce(Reducer<T> reducer);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user