Skip to content

Commit

Permalink
Ambiguous expressions now throw errors as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
lyncmi07 committed Aug 21, 2019
1 parent 472d0bb commit f4cd307
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/Com/NoSyn/Ast/If/Expression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,22 @@ validateAndGenerateD' programEnvironment possibleReturnTypes possibleParameterTy
Nothing -> return $ Right ((returnType functionOverload), compileString)

reduceOnPointerParameters::[FunctionOverload] -> [Set Ident] -> (Maybe FunctionOverload)
reduceOnPointerParameters [] _ = Nothing
reduceOnPointerParameters (x:xs) parameterTypes
| reduceOnPointerParameters' [z | (_, z) <- OrderMap.assocs $ parameters x] (Prelude.map (head.(Set.toList)) parameterTypes) = Just x
| otherwise = reduceOnPointerParameters xs parameterTypes
reduceOnPointerParameters functionOverloads parameterTypes =
let reducedFunctions = reduceOnPointerParameters' functionOverloads parameterTypes in
if (length reducedFunctions) == 1 then
let (finalFunction:_) = reducedFunctions in Just finalFunction
else Nothing

reduceOnPointerParameters'::[FunctionOverload] -> [Set Ident] -> [FunctionOverload]
reduceOnPointerParameters' [] _ = []
reduceOnPointerParameters' (x:xs) parameterTypes
| reduceOnPointerParameters'' [z | (_, z) <- OrderMap.assocs $ parameters x] (Prelude.map (head.(Set.toList)) parameterTypes) = x:(reduceOnPointerParameters' xs parameterTypes)
| otherwise = reduceOnPointerParameters' xs parameterTypes

reduceOnPointerParameters'::[Variable] -> [Ident] -> Bool
reduceOnPointerParameters' [] [] = True
reduceOnPointerParameters' (x:xs) (y:ys)
| getAlphaTypeName x == y = reduceOnPointerParameters' xs ys
reduceOnPointerParameters''::[Variable] -> [Ident] -> Bool
reduceOnPointerParameters'' [] [] = True
reduceOnPointerParameters'' (x:xs) (y:ys)
| getAlphaTypeName x == y = reduceOnPointerParameters'' xs ys
| otherwise = False

reduceParameterTypes::ProgramEnvironment -> [Set Ident] -> [Expression] -> CompilerStatus [(Either (Set Ident) (Ident, String))]
Expand Down Expand Up @@ -226,7 +233,7 @@ generateExpressionForPointerParameter _ _ expr = Error ((show expr) ++ " cannot

generateDFunctionCall::ProgramEnvironment -> Ident -> FunctionOverload -> [Either (Set Ident) (Ident, String)] -> [Expression] -> CompilerStatus String
generateDFunctionCall programEnvironment funcName functionOverload paramTypeEithers parameterExpressions = do
generatedParameters <- sequence $ Prelude.map (either (\x -> Error "Parameter expression could not be generated" (show x)) (\(_,x) -> return x)) paramTypeEithers
generatedParameters <- sequence $ Prelude.map (either (\x -> Error "Parameter expression could not be generated" (show (x, (funcName, functionOverload)))) (\(_,x) -> return x)) paramTypeEithers
zippedParameters <- (zipVariablesToParameterTypesAndExpressions (OrderMap.assocs (parameters functionOverload)) generatedParameters parameterExpressions)
addressWrappedParameters <- sequence $ Prelude.map (\((_,x),y, z) -> addressWrapper x y z) zippedParameters
generateDFunctionCall' programEnvironment funcName functionOverload addressWrappedParameters
Expand Down
26 changes: 26 additions & 0 deletions test/ambiguous_expression.ns.fail
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
%%SOURCE%%
native import std.stdio;

native Nothing writeln(String a);
native Nothing writeln(Int a);

Nothing main() {
aFunc(10);
aFunc("Hello");
aFunc(bFunc());

}

Nothing aFunc(String a) {
writeln("This was a string");
}
Nothing aFunc(Int a) {
writeln("This was an int");
}

String bFunc() {
"Hello";
}
Int bFunc() {
10;
}

0 comments on commit f4cd307

Please sign in to comment.