Example #1
0
// Parse English sentence and try and convert it into propositional logic
static String ParseEnglish(const String& input)
{
	std::cout << input << std::endl;

	EnglishTokenizer tokenizer;
	tokenizer.AddEndChars(";");
	tokenizer.AddKeyword("and");
	tokenizer.AddKeyword("or");
	tokenizer.AddKeyword("not");
	tokenizer.AddKeyword("then");
	tokenizer.LoadNouns("res/nounlist.txt");
	tokenizer.LoadAdjectives("res/adjectives.txt");
	tokenizer.AddWhitespaceChars(" \n\r\t");
	tokenizer.SetString(input);

	Statement* statement = new Statement();
	EnglishToken token;
	String string;
	while (tokenizer.Next(token))
	{
		switch (token.type)
		{
			case EnglishToken::Type::IDENTIFIER:
				statement->identifiers.push_back(token.token);
				string += token.token + " ";
				break;
			case EnglishToken::Type::OPERATOR:
				statement->operators.push_back(ParseEnglishOperator(token.token));
				Operator op = ParseEnglishOperator(token.token);
				string += OperatorToString(op);
				if (op != Operator::NEGATION)
					string += " ";
				break;
		}
	}
	std::cout << string << std::endl;
	delete statement;
	return string;
}
Example #2
0
const ErrorListRef BinaryExpression::Validate(
		const shared_ptr<ExecutionContext> execution_context,
		const_shared_ptr<TypeSpecifier> valid_left,
		const_shared_ptr<TypeSpecifier> valid_right) const {
	ErrorListRef errors = ErrorList::GetTerminator();

	const OperatorType op = GetOperator();

	auto left = GetLeft();
	auto left_errors = left->Validate(execution_context);
	if (ErrorList::IsTerminator(left_errors)) {
		auto left_type_specifier_result = left->GetTypeSpecifier(
				execution_context);
		auto left_type_specifier_errors =
				left_type_specifier_result.GetErrors();
		if (ErrorList::IsTerminator(left_type_specifier_errors)) {
			auto left_type_specifier = left_type_specifier_result.GetData();
			auto left_analysis = left_type_specifier->AnalyzeAssignmentTo(
					valid_left, execution_context->GetTypeTable());
			if (left_analysis != EQUIVALENT && left_analysis != UNAMBIGUOUS) {
				errors = ErrorList::From(
						make_shared<Error>(Error::SEMANTIC,
								Error::INVALID_LEFT_OPERAND_TYPE,
								left->GetLocation().begin,
								OperatorToString(op)), errors);
			}
		} else {
			errors = ErrorList::Concatenate(errors, left_type_specifier_errors);
		}
	} else {
		errors = ErrorList::Concatenate(errors, left_errors);
	}

	auto right = GetRight();
	auto right_errors = right->Validate(execution_context);
	if (ErrorList::IsTerminator(right_errors)) {
		auto right_type_specifier_result = GetRight()->GetTypeSpecifier(
				execution_context);
		auto right_type_specifier_errors =
				right_type_specifier_result.GetErrors();
		if (ErrorList::IsTerminator(right_type_specifier_errors)) {
			auto right_type_specifier = right_type_specifier_result.GetData();
			auto right_analysis = right_type_specifier->AnalyzeAssignmentTo(
					valid_right, execution_context->GetTypeTable());
			if (right_analysis != EQUIVALENT && right_analysis != UNAMBIGUOUS) {
				errors = ErrorList::From(
						make_shared<Error>(Error::SEMANTIC,
								Error::INVALID_RIGHT_OPERAND_TYPE,
								right->GetLocation().begin,
								OperatorToString(op)), errors);
			}
		} else {
			errors = ErrorList::Concatenate(errors,
					right_type_specifier_errors);
		}
	} else {
		errors = ErrorList::Concatenate(errors, right_errors);
	}

	return errors;
}