Package litil

Examples of litil.TypeScope


            boolean allExceptions = true;
            for (Expr.PatterMatching.Case pcase : patmat.cases) {
                //System.out.println("\thandling " + pcase);
                //unify pattern type and inputType
                //unify outcome types
                TypeScope newScope = env.child();
                Type patType = visit(pcase.pattern, newScope, nonGen);
                //System.out.println("scope after visiting pattern: " + newScope);
                //System.out.println("and patType=" + patType + " for pat " + pcase.pattern);
                //System.out.println("***************");
                //System.err.println("in " + inputType.getClass());
                if (!isException(inputType)) {
                    unify(patType, inputType);
                }
                //System.out.println("patType=" + patType + ", inputType=" + inputType);
                //System.out.println("$$$$$$$$$$$$$$$$");
                Type caseType = null;
                for (Instruction instruction : pcase.outcome) {
                    caseType = analyze(instruction, newScope, nonGen);
                }
                if (!isException(caseType)) {
                    unify(caseType, resType);
                    resType = caseType;
                    allExceptions = false;
                }

                //System.out.println("caseType=" + caseType + ", resType=" + resType);
                //System.out.println("%%%%%%%%%%%%%%%%%");

            }
            //System.out.println("resType=" + resType + " for " + patmat);
            return allExceptions ? Type.EXCEPTION : resType;
        } else if (node instanceof Expr.EThrow) {
            Expr.EThrow ethrow = (Expr.EThrow) node;
            Type exType = analyze(ethrow.exception, env, nonGen);
            unify(exType, Type.EXCEPTION);
            return Type.EXCEPTION;
        } else if (node instanceof Expr.TryCatch) {
            Expr.TryCatch tc = (Expr.TryCatch) node;


            Type tryType = null;
            for (Instruction instr : tc.tryBody) {
                tryType = analyze(instr, env, nonGen);
            }
            tryType = prune(tryType);
            System.err.println("" + tryType.getClass());
            System.err.println("TRY TYPE = " + (tryType == Type.EXCEPTION));
            Type.Variable resType = new Type.Variable();
            if (!isException(tryType)) {
                System.err.println("NOOOO");
                unify(tryType, resType);
            }


            for (Expr.PatterMatching.Case pcase : tc.catchCases) {
                TypeScope newScope = env.child();
                Type patType = visit(pcase.pattern, newScope, nonGen);
                unify(patType, Type.EXCEPTION);
                Type caseType = null;
                for (Instruction instruction : pcase.outcome) {
                    caseType = analyze(instruction, newScope, nonGen);
                }

                unify(caseType, resType);

            }

            return resType;
        } else if (node instanceof Expr.ELam) {
            Expr.ELam lam = (Expr.ELam) node;

            TypeScope newEnv = env.child();
            Set<Type> newNonGen = new HashSet<Type>(nonGen);
            List<Type> argTypes = new ArrayList<Type>();

            for (Named arg : lam.args) {
                Type argType = new Type.Variable();
                argTypes.add(argType);
                newEnv.define(arg.name, argType);
                newNonGen.add(argType);
            }
            Type resultType = null;
            for (Instruction instr : lam.instructions) {
                resultType = analyze(instr, newEnv, newNonGen);
            }
            if (lam.type != null) {
                unify(resultType, lam.type);
            }
            return Type.Function(argTypes, resultType);
        } else if (node instanceof LetBinding) {
            LetBinding let = (LetBinding) node;
            TypeScope newEnv = env.child();
            Set<Type> newNonGen = new HashSet<Type>(nonGen);
            List<Type> argTypes = new ArrayList<Type>();
            Type.Variable letTypeVar = new Type.Variable();

            //should a function be polymorphic to itself ?
            newEnv.define(let.name, letTypeVar);
            newNonGen.add(letTypeVar);
            for (Named arg : let.args) {
                Type argType = new Type.Variable();
                argTypes.add(argType);
                newEnv.define(arg.name, argType);
                newNonGen.add(argType);
            }
            Type resultType = null;
            for (Instruction instr : let.instructions) {
                resultType = analyze(instr, newEnv, newNonGen);
            }
            if (let.type != null) {
                unify(resultType, let.type);
            }
            Type letType = Type.Function(argTypes, resultType);
            env.define(let.name, letType);
            return letType;
        } else if (node instanceof DestructuringLetBinding) {
            DestructuringLetBinding let = (DestructuringLetBinding) node;
            TypeScope newEnv = env.child();
            Set<Type> newNonGen = new HashSet<Type>(nonGen);
            List<Type> argTypes = new ArrayList<Type>();
            Type letTypeVar = visit(let.main, newEnv, newNonGen);
            newNonGen.add(letTypeVar);
            for (Pattern arg : let.args) {
View Full Code Here


            unify(thenType, elseType);
            return thenType;
        } else if (node instanceof LetBinding) {
            LetBinding let = (LetBinding) node;
            TypeScope ss = new TypeScope(scope);
            List<Type> argTypes = new ArrayList<Type>();
            ss.define(let.name, new Type.Variable());

            for (Named arg : let.args) {
                Type argType = new Type.Variable();
                argTypes.add(argType);
                ss.define(arg.name, argType);
            }
            Type resultType = null;
            for (Instruction instr : let.instructions) {
                resultType = analyze(instr, ss);
            }
View Full Code Here

        super.lexer = lexer;
        prtDbg = false;
    }

    private static TypeScope trootScope() {
        TypeScope res = new TypeScope();
        res.define("+", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("add", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        //res.define("-", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("*", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        //res.define("/", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        //res.define("%", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        //res.define("=", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.BOOL));

        //res.define("int2str", Type.Function(Type.INT, Type.STR));
View Full Code Here

        System.out.println(ast.repr(1));

        ExplicitTypeChecker exTc = new ExplicitTypeChecker();
        HMTypeChecker hmTc = new HMTypeChecker();
        TypeScope tenv = trootScope().child();
        for (Instruction instr : ast.instructions) {
            System.out.println(instr.repr(0));

            System.out.println(":: " + hmTc.analyze(instr, tenv));
            System.out.println("--------------");
View Full Code Here

        p.instructions.addAll(body());
        return p;
    }

    private TypeScope rootScope() {
        TypeScope res = new TypeScope();
        res.define("+", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("-", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("*", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("/", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("%", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("=", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.BOOL));

        res.define("int2str", Type.Function(Type.INT, Type.STR));
        res.define("print", Type.Function(Type.STR, Type.UNIT));
        return res;
    }
View Full Code Here

public class BytecodeGen {
    public byte[] gen(Program p) {


        HMTypeChecker hm = new HMTypeChecker();
        TypeScope tenv = Prelude.trootScope().child();
        Names names = new Names();

        List<BcNode> bc = new ArrayList<BcNode>();
        for (Instruction inst : p.instructions) {
            bc.addAll(visitTopLevel(inst, names, hm, tenv));
View Full Code Here

        System.out.println("=============");

        if (tc) {
            System.out.println("Type checking ...");
            try {
                TypeScope typeEnv = trootScope().child();
                new HMTypeChecker().analyze(node, typeEnv);
                System.out.println("All is good");
            } catch (TypeError e) {
                System.err.println(e.getMessage());
                return;
View Full Code Here

        return p.program();
    }

    private static TypeScope trootScope() {
        TypeScope res = new TypeScope();
        res.define("-/1", Type.Function(Type.INT, Type.INT));
        res.define("+", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("-", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("*", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("/", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("%", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));

        res.define(">", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.BOOL));
        res.define("<", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.BOOL));

        Type.Variable a = new Type.Variable();
        res.define("=", Type.Function(Arrays.asList(a, a), Type.BOOL));

        res.define("int2str", Type.Function(Type.INT, Type.STR));
        res.define("print", Type.Function(Type.STR, Type.UNIT));
        res.define("error", Type.Function(Type.STR, new Type.Variable()));
        return res;
    }
View Full Code Here

            }
        };
    }

    public static TypeScope trootScope() {
        TypeScope res = new TypeScope();
        res.define("-/1", Type.Function(Type.INT, Type.INT));
        res.define("not", Type.Function(Type.BOOL, Type.BOOL));
        res.define("+", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("-", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("*", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("/", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));
        res.define("%", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.INT));

        res.define(">", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.BOOL));
        res.define("<", Type.Function(Arrays.asList(Type.INT, Type.INT), Type.BOOL));

        Type.Variable a = new Type.Variable();
        res.define("=", Type.Function(Arrays.asList(a, a), Type.BOOL));

        res.define("int2str", Type.Function(Type.INT, Type.STR));
        res.define("print", Type.Function(Type.STR, Type.UNIT));
        res.define("error", Type.Function(Type.STR, new Type.Variable()));

        Type.Variable b = new Type.Variable();
        Type listType = Type.List(b);
        List<DataDecl.TypeConstructor> listConstructors = new ArrayList<DataDecl.TypeConstructor>();
        List<Type> listVars = Arrays.<Type>asList(b);
        listConstructors.add(new DataDecl.TypeConstructor("Cons", Arrays.asList(b, listType)));
        listConstructors.add(new DataDecl.TypeConstructor("Nil"));
        DataDecl list = new DataDecl("List", listType, Arrays.asList(b), listConstructors);

        for (DataDecl.TypeConstructor tyCon : listConstructors) {
            res.define(tyCon.name, Type.TyCon(tyCon, list));
        }
        return res;
    }
View Full Code Here

        String pg2 = "let f x y = x + y\nlet a = 2\nlet b=3\nlet g = f a\ng b";
        AstNode node = parseFile("emb-list.ltl");
        System.out.println(node);
        System.out.println("=============");
        ev.dbgAp = false;
        TypeScope typeEnv = Prelude.trootScope().child();
        new HMTypeChecker().analyze(node, typeEnv);
        System.out.println("-------------------");
        System.out.println(typeEnv);
        ValScope valScope = Prelude.rootScope().child();
        System.out.println("--" + ev.eval(node, valScope));
View Full Code Here

TOP

Related Classes of litil.TypeScope

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.