/*
 * Encog(tm) Core v3.4 - Java Version
 * http://www.heatonresearch.com/encog/
 * https://github.com/encog/encog-java-core
 
 * Copyright 2008-2016 Heaton Research, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *   
 * For more information on Heaton Research copyrights, licenses 
 * and trademarks visit:
 * http://www.heatonresearch.com/copyright
 */
package org.encog.parse.expression.epl;

import java.util.StringTokenizer;

import org.encog.ml.ea.exception.EACompileError;
import org.encog.ml.prg.EncogProgram;
import org.encog.ml.prg.ProgramNode;
import org.encog.ml.prg.expvalue.ExpressionValue;
import org.encog.ml.prg.extension.EncogOpcodeRegistry;
import org.encog.ml.prg.extension.ProgramExtensionTemplate;
import org.encog.util.SimpleParser;
import org.encog.util.csv.CSVFormat;
import org.encog.util.datastruct.StackObject;

/**
 * Parse an EPL string.
 */
public class ParseEPL {
	private final EncogProgram holder;
	private SimpleParser parser;
	private StackObject nodeStack = new StackObject(100);
	
	public ParseEPL(final EncogProgram theHolder) {
		this.holder = theHolder;
	}
	
	public ProgramNode parse(final String expression) {
		this.parser = new SimpleParser(expression);
		
		while(!this.parser.eol()) {
			this.parser.eatWhiteSpace();
			
			// read in the command
			if( this.parser.readChar()!='[' ) {
				throw new EACompileError("Expected [");
			}
			this.parser.eatWhiteSpace();
			StringBuilder cmd = new StringBuilder();
			while(this.parser.peek()!=']' && !this.parser.eol()) {
				cmd.append(this.parser.readChar());
			}
			
			if( this.parser.peek()!=']') {
				throw new EACompileError("Expected ]");
			}
			this.parser.advance();
			
			
			// parse out the command
			StringTokenizer tok = new StringTokenizer(cmd.toString(),":");
			String name = tok.nextToken();
			int childCount = Integer.parseInt(tok.nextToken());
			ProgramExtensionTemplate temp = EncogOpcodeRegistry.INSTANCE.findOpcode(name, childCount);
			if( temp==null ) { 
				throw new EACompileError("Invalid instruction: " + name);
			}
			
			// build the arguments
			ProgramNode[] args = new ProgramNode[childCount];
			for(int i=args.length-1;i>=0;i--) {
				args[i] = this.nodeStack.pop();
			}
			
			// factor the node
			ProgramNode node = this.holder.getFunctions().factorProgramNode(name, this.holder, args);
			this.nodeStack.push(node);
			
			// add any needed data to the node
			for(int i=0;i 

Ads help maintain this website.