Skip to content

Commit

Permalink
Merge pull request #30 from lyncmi07/29_standard_alias_fix
Browse files Browse the repository at this point in the history
29 standard alias fix
  • Loading branch information
lyncmi07 authored Sep 1, 2019
2 parents c258b49 + f5f4b83 commit 10caf9f
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 81 deletions.
2 changes: 1 addition & 1 deletion src/Com/NoSyn/Ast/If/Constant.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ instance Typeable Constant where
getTypeNoCheck (CDouble _) = "Double"
getTypeNoCheck (CChar _) = "Char"
getTypeNoCheck (CString _) = "String"
getAlphaTypeName = getTypeNoCheck
getAlphaTypeName _ x = return $ getTypeNoCheck x

93 changes: 47 additions & 46 deletions src/Com/NoSyn/Ast/If/Expression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ instance TargetCodeGeneratable Expression where
generateD = generateExpression
instance Typeable Expression where
getTypeNoCheck (EConst constant) = getTypeNoCheck constant
getAlphaTypeName (EConst constant) = getAlphaTypeName constant
getAlphaTypeName pg (EConst constant) = getAlphaTypeName pg constant

parameterSetsFromPossibleFunctions::[FunctionOverload] -> [Expression] -> [Set Ident]
parameterSetsFromPossibleFunctions [] parameterExpressions = Prelude.take (length parameterExpressions) $ repeat Set.empty
Expand Down Expand Up @@ -74,7 +74,7 @@ lookupFunction funcName funcEnvironment =
possibleFunctionsFromReturnAndParamTypes::ProgramEnvironment -> Set Ident -> Ident -> [Set Ident] -> CompilerStatus [FunctionOverload]
possibleFunctionsFromReturnAndParamTypes programEnvironment possibleReturnTypes funcName possibleParameterTypes = do
possibleFunctionsByReturnType <- possibleFunctionsFromReturnTypes programEnvironment possibleReturnTypes funcName (length possibleParameterTypes)
let filteredPossibleFunctions = Prelude.map (\(x,_) -> x) $ Prelude.filter validFunctionPredicate $ zip possibleFunctionsByReturnType (repeat possibleParameterTypes) in
let filteredPossibleFunctions = Prelude.map (\(x,_) -> x) $ Prelude.filter (validFunctionPredicate programEnvironment) $ zip possibleFunctionsByReturnType (repeat possibleParameterTypes) in
if (length filteredPossibleFunctions) == 0 then
Error ("there are no function overloads for '"
++ funcName
Expand All @@ -85,25 +85,26 @@ possibleFunctionsFromReturnAndParamTypes programEnvironment possibleReturnTypes
else return $ filteredPossibleFunctions


validFunctionPredicate::(FunctionOverload, [Set Ident]) -> Bool
validFunctionPredicate (FO { parameters = parameterMap }, parameterTypeSets) =
validFunctionPredicate' (OrderMap.assocs parameterMap) parameterTypeSets
validFunctionPredicate::ProgramEnvironment -> (FunctionOverload, [Set Ident]) -> Bool
validFunctionPredicate pg (FO { parameters = parameterMap }, parameterTypeSets) =
validFunctionPredicate' pg (OrderMap.assocs parameterMap) parameterTypeSets

validFunctionPredicate'::[(Ident, Variable)] -> [Set Ident] -> Bool
validFunctionPredicate' [] [] = True
validFunctionPredicate' ((_, (VVariadic paramType _)):[]) [] = True
validFunctionPredicate' _ [] = False
validFunctionPredicate' [] _ = False
validFunctionPredicate' ((_, (VConst x _)):xs) (y:ys)
| (x `Set.member` y) || ((x ++ "PTR") `Set.member` y) = validFunctionPredicate' xs ys
validFunctionPredicate'::ProgramEnvironment -> [(Ident, Variable)] -> [Set Ident] -> Bool
validFunctionPredicate' _ [] [] = True
validFunctionPredicate' _ ((_, (VVariadic paramType _)):[]) [] = True
validFunctionPredicate' _ _ [] = False
validFunctionPredicate' _ [] _ = False
validFunctionPredicate' pg ((_, (VConst x _)):xs) (y:ys)
| (x `Set.member` y) || ((x ++ "PTR") `Set.member` y) = validFunctionPredicate' pg xs ys
| otherwise = False
validFunctionPredicate' ((_, (VPointer x _)):xs) (y:ys)
| (x `Set.member` y) || ((x ++ "PTR") `Set.member` y) = validFunctionPredicate' xs ys
| otherwise = False
validFunctionPredicate' (x@(_, paramVariable@(VVariadic paramType _)):[]) (y:ys)
| paramType `Set.member` y = validFunctionPredicate' [x] ys
| (getAlphaTypeName paramVariable) `Set.member` y = (length ys) == 0
validFunctionPredicate' pg ((_, (VPointer x _)):xs) (y:ys)
| (x `Set.member` y) || ((x ++ "PTR") `Set.member` y) = validFunctionPredicate' pg xs ys
| otherwise = False
validFunctionPredicate' pg (x@(_, paramVariable@(VVariadic paramType _)):[]) (y:ys)
| paramType `Set.member` y = validFunctionPredicate' pg [x] ys
| otherwise = case (getAlphaTypeName (aliases pg) paramVariable) >>= (\x -> return $ x `Set.member` y) of
Valid _ x -> x && ((length ys) == 0)
_ -> False

generateExpression::ProgramEnvironment -> Expression -> CompilerStatus String
generateExpression programEnvironment functionCall@(EFuncCall _ _) = do
Expand All @@ -128,11 +129,11 @@ generateExpressionForConstParameter _ generatedExpression _ = return generatedEx

validateAndGenerateD::ProgramEnvironment -> Set Ident -> Expression -> CompilerStatus (Either (Set Ident) (Ident, String))
validateAndGenerateD programEnvironment returnTypes functionCall@(EFuncCall funcName paramExprs) = do
possibleFunctions <- possibleFunctionsFromReturnTypes programEnvironment nonPointerReturnTypes funcName (length paramExprs)
unaliasedNonPointerReturnTypes <- sequence [lookupAtomicNoSynType x (aliases programEnvironment) | x <- (Data.Set.toList returnTypes), backTake 3 x /= "PTR"]
possibleFunctions <- possibleFunctionsFromReturnTypes programEnvironment (Data.Set.fromList unaliasedNonPointerReturnTypes) funcName (length paramExprs)
let parameterSets = parameterSetsFromPossibleFunctions possibleFunctions paramExprs in
validateAndGenerateD' programEnvironment nonPointerReturnTypes parameterSets functionCall
validateAndGenerateD' programEnvironment (Data.Set.fromList unaliasedNonPointerReturnTypes) parameterSets functionCall
where
nonPointerReturnTypes = Data.Set.fromList $ Prelude.filter (\x -> backTake 3 x /= "PTR") (Data.Set.toList returnTypes)
backTake x = reverse.(Prelude.take x).reverse

validateAndGenerateD programEnvironment returnTypes (EConst const) = do
Expand All @@ -144,10 +145,10 @@ validateAndGenerateD programEnvironment returnTypes (EConst const) = do
validateAndGenerateD programEnvironment returnTypes (EIdent varName) = do
variable <- lookupVariableType programEnvironment varName
variableType <- getNoSynType programEnvironment variable
let alphaVariableType = getAlphaTypeName variable in
if variableType `Set.member` returnTypes
then return $ Right (alphaVariableType, varName)
else Error ("Identifier '" ++ varName ++ "' given cannot be used in context") (show returnTypes)
alphaVariableType <- getAlphaTypeName (aliases programEnvironment) variable
if variableType `Set.member` returnTypes
then return $ Right (alphaVariableType, varName)
else Error ("Identifier '" ++ varName ++ "' given cannot be used in context") (show returnTypes)

validateAndGenerateD'::ProgramEnvironment -> Set Ident -> [Set Ident] -> Expression -> CompilerStatus (Either (Set Ident) (Ident, String))
validateAndGenerateD' programEnvironment possibleReturnTypes possibleParameterTypes functionCall@(EFuncCall funcName paramExpressions) = do
Expand All @@ -160,7 +161,7 @@ validateAndGenerateD' programEnvironment possibleReturnTypes possibleParameterTy
if (length possibleFunctions) == 1 then
Just $ head possibleFunctions
else let reducedParameterTypes = parameterTypesFromEithers reducedParameterTypeEithers in
reduceOnPointerParameters possibleFunctions reducedParameterTypes in
reduceOnPointerParameters programEnvironment possibleFunctions reducedParameterTypes in
case maybeFuncToGenerate of
Just funcToGenerate ->
let finalReducedParameterTypes = parameterSetsFromPossibleFunctions possibleFunctions paramExpressions in
Expand All @@ -178,23 +179,24 @@ validateAndGenerateD' programEnvironment possibleReturnTypes possibleParameterTy
(Just dependency) -> dependencyRequired dependency $ Right ((returnType functionOverload), compileString)
Nothing -> return $ Right ((returnType functionOverload), compileString)

reduceOnPointerParameters::[FunctionOverload] -> [Set Ident] -> (Maybe FunctionOverload)
reduceOnPointerParameters functionOverloads parameterTypes =
let reducedFunctions = reduceOnPointerParameters' functionOverloads parameterTypes in
reduceOnPointerParameters::ProgramEnvironment -> [FunctionOverload] -> [Set Ident] -> (Maybe FunctionOverload)
reduceOnPointerParameters pg functionOverloads parameterTypes =
let reducedFunctions = reduceOnPointerParameters' pg 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'::ProgramEnvironment -> [FunctionOverload] -> [Set Ident] -> [FunctionOverload]
reduceOnPointerParameters' _ [] _ = []
reduceOnPointerParameters' pg (x:xs) parameterTypes
| reduceOnPointerParameters'' pg [z | (_, z) <-
OrderMap.assocs $ parameters x] (Prelude.map (head.(Set.toList)) parameterTypes) = x:(reduceOnPointerParameters' pg xs parameterTypes)
| otherwise = reduceOnPointerParameters' pg xs parameterTypes

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

reduceParameterTypes::ProgramEnvironment -> [Set Ident] -> [Expression] -> CompilerStatus [(Either (Set Ident) (Ident, String))]
Expand Down Expand Up @@ -250,15 +252,14 @@ generateDFunctionCall programEnvironment funcName functionOverload paramTypeEith
addressWrapper (VVariadic _ _) x y = generateExpressionForConstParameter programEnvironment x y

generateDFunctionCall'::ProgramEnvironment->Ident->FunctionOverload->[String]->CompilerStatus String
generateDFunctionCall' programEnvironment funcName (FO { returnType=returnType, parameters=parameters }) parameterExpressions =
let joinedParameterExpressions = concat $ intersperse "," parameterExpressions in
return $ fullDName ++ "(" ++ joinedParameterExpressions ++ ")"
where
parameterTypes = [ getAlphaTypeName x | (_, x) <- OrderMap.assocs parameters ]
fullDName = case functionIsNative programEnvironment funcName of
generateDFunctionCall' programEnvironment funcName (FO { returnType=returnType, parameters=parameters }) parameterExpressions = do
parameterTypes <- sequence $ [ getAlphaTypeName (aliases programEnvironment) x | (_, x) <- OrderMap.assocs parameters ]
let fullDName = (case functionIsNative programEnvironment funcName of
(Just postfix) -> dropPostfix postfix funcName
Nothing -> let dFuncSuffix = returnType ++ (concat parameterTypes) in
funcName ++ "_" ++ dFuncSuffix
Nothing -> let dFuncSuffix = returnType ++ (concat parameterTypes) in funcName ++ "_" ++ dFuncSuffix) in
let joinedParameterExpressions = concat $ intersperse "," parameterExpressions in
return $ fullDName ++ "(" ++ joinedParameterExpressions ++ ")"
where


dropPostfix postfix = (reverse.(Prelude.drop $ length postfix).reverse)
Expand Down
12 changes: 6 additions & 6 deletions src/Com/NoSyn/Ast/If/FunctionDefinition.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ instance DIdentifiable FunctionDefinition where
getDIdentifier _ (FDNative funcName _ _) = return funcName
getDIdentifier programEnvironment func@(FDNoSyn funcName returnType parameters _) = do
noSynReturnType <- getNoSynType programEnvironment func
let parameterNoSynTypes = Prelude.map getAlphaTypeName (Listable.toList parameters) in
return $ funcName
++ "_"
++ noSynReturnType
++ concat parameterNoSynTypes
parameterNoSynTypes <- sequence $ Prelude.map (getAlphaTypeName (aliases programEnvironment)) (Listable.toList parameters)
return $ funcName
++ "_"
++ noSynReturnType
++ concat parameterNoSynTypes

instance Nameable FunctionDefinition where
getName (FDNative name _ _) = name
Expand All @@ -58,4 +58,4 @@ instance Nameable FunctionDefinition where
instance Typeable FunctionDefinition where
getTypeNoCheck (FDNative _ returnType _) = returnType
getTypeNoCheck (FDNoSyn _ returnType _ _) = returnType
getAlphaTypeName = getTypeNoCheck
getAlphaTypeName _ x = return $ getTypeNoCheck x
34 changes: 21 additions & 13 deletions src/Com/NoSyn/Ast/If/Parameter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Com.NoSyn.Ast.Traits.Listable as Listable
import Data.List
import Data.Map
import Com.NoSyn.Environment.ProgramEnvironment
import Com.NoSyn.Environment.AliasEnvironment
import Com.NoSyn.Error.CompilerStatus

type Parameters = Block Parameter
Expand Down Expand Up @@ -48,21 +49,28 @@ instance Typeable Parameter where
getTypeNoCheck (PConst paramType _) = paramType
getTypeNoCheck (PPointer paramType _) = paramType ++ "*"
getTypeNoCheck (PVariadic paramType _) = paramType ++ "..."
getAlphaTypeName (PConst paramType _) = paramType
getAlphaTypeName (PPointer paramType _) = paramType ++ "PTR"
getAlphaTypeName (PVariadic paramType _) = paramType ++ "VARAD"
getAlphaTypeName aliasEnvironment (PConst paramType _) = lookupAtomicNoSynType paramType aliasEnvironment
getAlphaTypeName aliasEnvironment (PPointer paramType _) = do
atomicTypeName <- lookupAtomicNoSynType paramType aliasEnvironment
return $ atomicTypeName ++ "PTR"
getAlphaTypeName aliasEnvironment (PVariadic paramType _) = do
atomicTypeName <- lookupAtomicNoSynType paramType aliasEnvironment
return $ atomicTypeName ++ "VARAD"

instance Blockable Parameter where
blockSeparator _ = ", "

parameterToTuple::Parameter -> (Ident, Variable)
parameterToTuple (PConst paramType paramName) =
(paramName , (VConst paramType paramName))
parameterToTuple (PPointer paramType paramName) =
(paramName , (VPointer paramType paramName))
parameterToTuple (PVariadic paramType paramName) =
(paramName, (VVariadic paramType paramName))
parameterToTuple::AliasEnvironment -> Parameter -> CompilerStatus (Ident, Variable)
parameterToTuple aliasEnvironment (PConst paramType paramName) = do
realParamType <- lookupAtomicNoSynType paramType aliasEnvironment
return (paramName , (VConst realParamType paramName))
parameterToTuple aliasEnvironment (PPointer paramType paramName) = do
realParamType <- lookupAtomicNoSynType paramType aliasEnvironment
return (paramName , (VPointer realParamType paramName))
parameterToTuple aliasEnvironment (PVariadic paramType paramName) = do
realParamType <- lookupAtomicNoSynType paramType aliasEnvironment
return (paramName, (VVariadic realParamType paramName))

parametersToTuples::Parameters -> [(Ident, Variable)]
parametersToTuples parameters =
Prelude.map parameterToTuple (Listable.toList parameters)
parametersToTuples::AliasEnvironment -> Parameters -> CompilerStatus [(Ident, Variable)]
parametersToTuples aliasEnvironment parameters =
sequence $ Prelude.map (parameterToTuple aliasEnvironment) (Listable.toList parameters)
4 changes: 3 additions & 1 deletion src/Com/NoSyn/Ast/If/VariableDeclaration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ instance EnvironmentUpdater VariableDeclaration where

instance Typeable VariableDeclaration where
getTypeNoCheck (VDec varType _) = varType
getAlphaTypeName (VDec varType _) = varType
getAlphaTypeName aliasEnvironment (VDec varType _) = do
atomicVarType <- lookupAtomicNoSynType varType aliasEnvironment
return atomicVarType
3 changes: 2 additions & 1 deletion src/Com/NoSyn/Ast/Traits/Typeable.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Com.NoSyn.Ast.Traits.Typeable where

import Com.NoSyn.Error.CompilerStatus
import Com.NoSyn.Environment.AliasEnvironment

class Typeable a where
getTypeNoCheck :: a -> String
getAlphaTypeName :: a -> String
getAlphaTypeName :: AliasEnvironment -> a -> CompilerStatus String
6 changes: 3 additions & 3 deletions src/Com/NoSyn/Data/Variable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ instance Typeable Variable where
getTypeNoCheck (VConst varType _) = varType
getTypeNoCheck (VPointer varType _) = varType
getTypeNoCheck (VVariadic varType _) = varType
getAlphaTypeName (VConst varType _) = varType
getAlphaTypeName (VPointer varType _) = varType ++ "PTR"
getAlphaTypeName (VVariadic varType _) = varType ++ "VARAD"
getAlphaTypeName _ (VConst varType _) = return $ varType
getAlphaTypeName _ (VPointer varType _) = return $ varType ++ "PTR"
getAlphaTypeName _ (VVariadic varType _) = return $ varType ++ "VARAD"

instance Nameable Variable where
getName (VConst _ name) = name
Expand Down
16 changes: 8 additions & 8 deletions src/Com/NoSyn/Evaluation/Program/Internal/FunctionEvaluation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ programFunctionDefinitionEvaluate' aliasEnvironment (_:xs) currentFunctionEnviro
createFunctionEntry::AliasEnvironment -> FunctionDefinition -> CompilerStatus (Ident, FunctionOverload)
createFunctionEntry aliasEnvironment (FDNative functionName returnType parameters)= do
_ <- lookupDType returnType aliasEnvironment
_ <- sequence $ Prelude.map (\(_, x) -> lookupDType (getTypeNoCheck x) aliasEnvironment) (parametersToTuples parameters)
return (functionName, FO { returnType = returnType, parameters = parameterOMap, parentModule = Nothing })
where
parameterOMap = (Data.Map.Ordered.fromList (parametersToTuples parameters))
unaliasedParameterList <- parametersToTuples aliasEnvironment parameters
unaliasedReturnType <- lookupAtomicNoSynType returnType aliasEnvironment
_ <- sequence $ Prelude.map (\(_, x) -> lookupDType (getTypeNoCheck x) aliasEnvironment) unaliasedParameterList
return (functionName, FO { returnType = unaliasedReturnType, parameters = (Data.Map.Ordered.fromList unaliasedParameterList), parentModule = Nothing })
createFunctionEntry aliasEnvironment (FDNoSyn functionName returnType parameters _)= do
_ <- lookupDType returnType aliasEnvironment
_ <- sequence $ Prelude.map (\(_, x) -> lookupDType (getTypeNoCheck x) aliasEnvironment) (parametersToTuples parameters)
return (functionName, FO { returnType = returnType, parameters = parameterOMap, parentModule = Nothing})
where
parameterOMap = (Data.Map.Ordered.fromList (parametersToTuples parameters))
unaliasedParameterList <- parametersToTuples aliasEnvironment parameters
unaliasedReturnType <- lookupAtomicNoSynType returnType aliasEnvironment
_ <- sequence $ Prelude.map (\(_, x) -> lookupDType (getTypeNoCheck x) aliasEnvironment) unaliasedParameterList
return (functionName, FO { returnType = unaliasedReturnType, parameters = (Data.Map.Ordered.fromList unaliasedParameterList), parentModule = Nothing})


4 changes: 2 additions & 2 deletions test/aliases.ns.disable → test/aliases.ns
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Nothing infix_:=_(Int* a, AnotherInt b) {
assignment(b, b);
}

Nothing infix_+_(Int* a, AnotherInt b) {
Nothing infix_+_(AnotherInt* a, AnotherInt b) {
a := add(a, b);
}

Int add(Int a, Int b) {
Int add(Int a, AnotherInt b) {
a;
}
8 changes: 8 additions & 0 deletions test/aliases_methods_only.ns
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%%SOURCE%%

native AnotherInt foo();
alias AnotherInt = Int;

AnotherInt add(AnotherInt a, AnotherInt b) {
foo();
}

0 comments on commit 10caf9f

Please sign in to comment.