Package wyvern.tools.types

Examples of wyvern.tools.types.Environment


    if (!typeGuard) {
      for (TypedAST ast : getInnerIterable()) {
        if (ast instanceof ImportDeclaration) {
          importEnv.set(((ImportDeclaration) ast).extendType(importEnv.get(), Globals.getStandardEnv()));
        } else if (ast instanceof EnvironmentExtender) {
          Environment delta = ((EnvironmentExtender) ast).extendType(Environment.getEmptyEnvironment(), importEnv.get().extend(Globals.getStandardEnv()));
          dclEnv.set(dclEnv.get().extend(delta));
          delta.getBindings().stream()
              .flatMap(bndg -> (bndg instanceof TypeBinding)? Stream.of((TypeBinding)bndg) : Stream.empty())
              .forEach(bndg -> typeEnv.set(typeEnv.get().extend(bndg)));
        }
      }
      typeGuard = true;
View Full Code Here


  }

  @Override
  public void evalDecl(Environment evalEnv, Environment declEnv) {
    ValueBinding selfBinding = (ValueBinding) declEnv.lookup(name);
    Environment objEnv = Environment.getEmptyEnvironment();
    Value selfV = new Obj(inner.evalDecl(objEnv));
    selfBinding.setValue(selfV);
  }
View Full Code Here

    ClassBinding classVarTypeBinding = (ClassBinding) env.lookupBinding("class", ClassBinding.class).orElse(null);

    // System.out.println("classVarTypeBinding = " + classVarTypeBinding);

    if (classVarTypeBinding != null) { //In a class method
      Environment declEnv = classVarTypeBinding.getClassDecl().getInstanceMembersEnv();
      Environment innerEnv = seq.extendName(Environment.getEmptyEnvironment(), env).extend(declEnv);
      seq.typecheck(env.extend(new NameBindingImpl("this", new ClassType(new Reference<>(innerEnv), new Reference<>(innerEnv), new LinkedList<>(), classVarTypeBinding.getClassDecl().getName()))), Optional.empty());


      Environment environment = seq.extendType(declEnv, declEnv.extend(env));
      environment = seq.extendName(environment, environment.extend(env));
      Environment nnames = seq.extend(environment, environment);

      Environment objTee = TypeDeclUtils.getTypeEquivalentEnvironment(nnames.extend(declEnv));
      Type classVarType = new ClassType(new Reference<>(nnames.extend(declEnv)), new Reference<>(objTee), new LinkedList<>(), classVarTypeBinding.getClassDecl().getName());
      if (!(classVarType instanceof ClassType)) {
        // System.out.println("Type checking classVarType: " + classVarType + " and clsVar = " + clsVar);
        ToolError.reportError(ErrorMessage.MUST_BE_LITERAL_CLASS, this, classVarType.toString());
      }

      // TODO SMELL: do I really need to store this?  Can get it any time from the type
      cls = classVarTypeBinding.getClassDecl();
      ct = classVarType;

      return classVarType;
    } else { // Standalone
     
      isGeneric = true;
      Environment innerEnv = seq.extendType(Environment.getEmptyEnvironment(), env);
      Environment savedInner = env.extend(innerEnv);
      innerEnv = seq.extendName(innerEnv, savedInner);

      Environment declEnv = env.extend(new NameBindingImpl("this", new ClassType(new Reference<>(innerEnv), new Reference<>(innerEnv), new LinkedList<>(), null)));
      final Environment ideclEnv = StreamSupport.stream(seq.getDeclIterator().spliterator(), false).
          reduce(declEnv, (oenv,decl)->(decl instanceof ClassDeclaration)?decl.extend(oenv, savedInner):oenv,(a,b)->a.extend(b));
      seq.getDeclIterator().forEach(decl -> decl.typecheck(ideclEnv, Optional.<Type>empty()));


      Environment mockEnv = Environment.getEmptyEnvironment();

      LinkedList<Declaration> decls = new LinkedList<>();

      mockEnv = getGenericDecls(env, mockEnv, decls);
      Environment nnames = (seq.extendName(mockEnv,mockEnv.extend(env)));

      ClassDeclaration classDeclaration = new ClassDeclaration("generic" + generic_num++, "", "", new DeclSequence(decls), mockEnv, new LinkedList<String>(), getLocation());
      cls = classDeclaration;
      Environment tee = TypeDeclUtils.getTypeEquivalentEnvironment(nnames.extend(mockEnv));
     
      ct = new ClassType(new Reference<>(nnames.extend(mockEnv)), new Reference<>(tee), new LinkedList<String>(), null);
      return ct;
    }
  }
View Full Code Here

    return mockEnv;
  }

  @Override
  public Value evaluate(Environment env) {
    Environment argValEnv = Environment.getEmptyEnvironment();
    for (Entry<String, TypedAST> elem : args.entrySet())
      argValEnv = argValEnv.extend(new ValueBinding(elem.getKey(), elem.getValue().evaluate(env)));

    ClassBinding classVarTypeBinding = (ClassBinding) env.lookupBinding("class", ClassBinding.class).orElse(null);
    ClassDeclaration classDecl;
    if (classVarTypeBinding != null)
      classDecl = classVarTypeBinding.getClassDecl();
    else {

      Environment mockEnv = Environment.getEmptyEnvironment();

      LinkedList<Declaration> decls = new LinkedList<>();

      mockEnv = getGenericDecls(env, mockEnv, decls);

      classDecl = new ClassDeclaration("generic" + generic_num++, "", "", new DeclSequence(), mockEnv, new LinkedList<String>(), getLocation());
    }

    AtomicReference<Value> objRef = new AtomicReference<>();
    Environment evalEnv = env.extend(new LateValueBinding("this", objRef, ct));
    classDecl.evalDecl(evalEnv, classDecl.extendWithValue(Environment.getEmptyEnvironment()));
    final Environment ideclEnv = StreamSupport.stream(seq.getDeclIterator().spliterator(), false).
        reduce(evalEnv, (oenv,decl)->(decl instanceof ClassDeclaration)?decl.evalDecl(oenv):oenv, Environment::extend);
    Environment objenv = seq.bindDecls(ideclEnv, seq.extendWithDecls(classDecl.getFilledBody(objRef)));
    objRef.set(new Obj(objenv.extend(argValEnv)));
   
    // System.out.println("Finished evaluating new: " + this);
   
    return objRef.get();
  }
View Full Code Here

    return new TypeDeclaration(nameBinding.getName(), (DeclSequence)newChildren.get("decls"), metaValue, location);
  }

  @Override
  public Type doTypecheck(Environment env) {
    Environment eenv = decls.extend(env, env);

   
    for (Declaration decl : decls.getDeclIterator()) {
      decl.typecheckSelf(eenv);
    }
View Full Code Here

    return this.typeBinding.getType();
 
 
  @Override
  protected Environment doExtend(Environment old, Environment against) {
    Environment newEnv = old.extend(nameBinding).extend(typeBinding);
    // newEnv = newEnv.extend(new NameBindingImpl("this", nameBinding.getType())); // Why is there "this" in a type (not class)?
   
    //extend with tag information
    if (isTagged()) {
      //type-test the tag information
     
      //TODO: fix this
     
      //first get/ create the binding
      TagBinding tagBinding = TagBinding.getOrCreate(taggedInfo.getTagName());
      newEnv = newEnv.extend(tagBinding);
     
      //now handle case-of and comprises clauses
      if (taggedInfo.getCaseOfTag() != null) {
        String caseOf = taggedInfo.getCaseOfTag();
       
View Full Code Here

    return newEnv;
  }

  @Override
  public Environment extendWithValue(Environment old) {
    Environment newEnv = old.extend(new ValueBinding(nameBinding.getName(), nameBinding.getType()));
    return newEnv;
  }
View Full Code Here

    return new DefDeclaration(name, type, argNames, newChildren.get("body"), isClass, location, true);
  }

  @Override
  protected Type doTypecheck(Environment env) {
    Environment extEnv = env;
    for (NameBinding bind : argNames) {
      extEnv = extEnv.extend(bind);
    }
    if (body != null) {
      Type bodyType = body.typecheck(extEnv, Optional.of(((Arrow)type).getResult())); // Can be null for def inside type!
      type = TypeResolver.resolve(type, env);
     
View Full Code Here

    return body;
  }

  @Override
  public Environment extendWithValue(Environment old) {
    Environment newEnv = old.extend(new ValueBinding(name, type));
    return newEnv;
  }
View Full Code Here

    return old.extend(binding);
  }

  @Override
  public Environment extendWithValue(Environment old) {
    Environment newEnv = old.extend(new VarValueBinding(binding.getName(), binding.getType(), null));
    return newEnv;
    //Environment newEnv = old.extend(new ValueBinding(binding.getName(), defValue));
  }
View Full Code Here

TOP

Related Classes of wyvern.tools.types.Environment

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.