Beispiel #1
0
int nextSyntaxToken(LangContext & context,int term, int pos)
{
	if(isOperatorToken(context.data,pos)) return pos+1;
	else if(isPrimaryToken(context.data,pos)) return pos+2;
	else if(context.data[pos]==synParam) return pos+2;
	else if(context.data[pos]==synSyntax) return pos+2;
	else if(context.data[pos]==synAssign) return pos+2;
	else if(context.data[pos]==synGuard) return pos+1;
	else if(context.data[pos]==synConstructor)
	{
		while(context.data[pos])pos++;
		return pos+1;
	}
	else if(context.data[pos]==synVariant)
	{
		while(context.data[pos])pos++;
		return pos+1;
	}
	else if(pos && context.data[pos] == synList)
	{
		pos++;
		//eat body
		while(context.data[pos] != synNill)pos = nextSyntaxToken(context, term, pos);
		pos++;
		//eat tail
		while(context.data[pos] != synNill)pos = nextSyntaxToken(context, term, pos);
		return pos + 1;
	}
	else if(pos && context.data[pos] == synOneOf)
	{
		pos++;
		while(true)
		{
			while(context.data[pos] != synNill)pos = nextSyntaxToken(context, term, pos);
			pos++;
			if(context.data[pos] == synNill)break;
		}
		assert(context.data[pos] == synNill);
		return pos + 1;
	}
	else if(pos && context.data[pos] == synOption)
	{
		pos++;
		while(context.data[pos] != synNill)pos = nextSyntaxToken(context, term, pos);
		return pos + 1; 
	}
	return pos+1;
}
Beispiel #2
0
	Compiler::llvmObject Compiler::compile(parsing::AST ast)
	{
		TRACE_INDATA(ast.display());

		for (unsigned int i = 0; i < ast.size(); i++)
		{
			if (ast[i].size() == 0 && symbols.find(ast[i].getContent().get()) != symbols.end())
			{
				TRACE_COUT("found symbol - " << ast[i].getContent().get() << "\n");
				if (ast[i+1].size() == 0 && ast[i+1].getContent().get().compare("(") == 0)
				{
					TRACE_COUT("  with arguments...\n");
					vector<parsing::AST> args = tokenizeAST(ast[i+2], ",");
					for (vector<parsing::AST>::iterator j = args.begin(); j != args.end(); j++)
					{
						if (j->size() == 0)
						{
							errors.push_back(parsing::ASTError(ast[i+1].getContent(), "empty argument"));
							return llvmObjectNull();
						}
						else
						{
							llvmObject tmp = compile(*j);
						}
					}

					if (ast[i+3].size() != 0 || ast[i+3].getContent().get().compare(")") != 0)
					{
						errors.push_back(parsing::ASTError(ast[i+3].getContent(), "expected a right parenthesis"));
					}

					if (ast[i+4].size() != 0 || ast[i+4].getContent().get().compare(";") != 0)
					{
						errors.push_back(parsing::ASTError(ast[i+4].getContent(), "expected a semicolon"));
					}
				}
				else if (ast[i+1].size() == 0 && ast[i+1].getContent().get().compare(";") == 0)
				{
					TRACE_COUT("  line ends...\n");
				}
				else if (ast[i+1].size() == 0 && ast[i+1].getContent().get().compare("=") == 0)
				{
					TRACE_COUT("found assignment of expression - " << ast[i+1].getContent().get() << "\n");
				}
				else
				{
					errors.push_back(parsing::ASTError(ast[i+1].getContent(), "unexpected token"));
					return llvmObjectNull();
				}
			}
			else if (ast[i].size() == 0 && types.find(ast[i].getContent().get()) != types.end())
			{
				TRACE_COUT("found type - " << ast[i].getContent().get() << "\n");
				for (i++; i < ast.size(); i++)
				{
					if (ast[i].size() == 0 && types.find(ast[i].getContent().get()) != types.end())
					{
						TRACE_COUT("found type - " << ast[i].getContent().get() << "\n");
					}
					else if (ast[i].size() == 0 && ast[i].getContent().get().compare("(") == 0)
					{
						if (ast[i+1].size() == 0)
						{
							errors.push_back(parsing::ASTError(ast[i].getContent(), "expected scope, but got a leaf"));
							return llvmObjectNull();
						}
						else
						{
							llvmObject tmp = compile(ast[i+1]);
						}

						if (ast[i+2].size() != 0 || ast[i+2].getContent().get().compare(")") != 0)
						{
							errors.push_back(parsing::ASTError(ast[i+2].getContent(), "expected a left parenthesis"));
							return llvmObjectNull();
						}
					}
					else if (ast[i].size() == 0 && isSymbolToken(ast[i].getContent()))
					{
						TRACE_COUT("found symbol after types - " << ast[i].getContent().get() << "\n");
						addSymbol(ast[i].getContent().get());
						break;
					}
					else
					{
						errors.push_back(parsing::ASTError(ast[i].getContent(), "expected either type, type expression, or an expression symbol"));
						return llvmObjectNull();
					}
				}
			}
			else if (ast[i].size() == 0 && isValueToken(ast[i].getContent()))
			{
				TRACE_COUT("found value - " << ast[i].getContent().get() << "\n");
			}
			else if (ast[i].size() == 0 && isOperatorToken(ast[i].getContent()))
			{
				TRACE_COUT("found operator - " << ast[i].getContent().get() << "\n");
				if (ast[i].getContent().get().compare("!") == 0 || ast[i].getContent().get().compare("~") == 0)
				{
					TRACE_COUT("  unary right operation\n");
				}
				else if (ast[i].getContent().get().compare("++") == 0 || ast[i].getContent().get().compare("--") == 0)
				{
					TRACE_COUT("  unary left operation\n");
				}
				else
				{
					TRACE_COUT("  binary operation\n");
				}
			}
		}

		return llvmObjectNull();
	}