Package scriptingLanguage

Examples of scriptingLanguage.Token


   */
  @Override
  public Token eval(AbstractClass<?> caller, Token parameters, AbstractFrame frame) throws ArrayIndexOutOfBoundsException, IllegalArgumentException, InterpreterException {
    int index = (int) ((Variable<Integer>) parameters.getCar()).getSource();
    if ((parameters = parameters.getNextToken()).isNull() || parameters.getCar().equals("get"))
      return new Token(Array.get(getSource(), index), ((Variable<?>) Array.get(getSource(), index)).getType().getTokenType());
    if (parameters.getCar().equals("set")) {
      if (((Variable<?>) (parameters = parameters.getNextToken()).getCar()).getType().extend((AbstractClass<?>) getType().getSource()) == -1)
        throw new InvalidAssignmentException("Cannot assign a value of the type, " + ((Variable<?>) parameters.getCar()).getType() + " to " + getType().getSource());
      Array.set(getSource(), index, parameters.getCar());
      return new Token(Interpreter.NULL, Interpreter.voidType);
    }
    return ((Variable<?>) Array.get(getSource(), index)).eval(caller, parameters, frame);
  }
View Full Code Here


 
  public static Pair<String, InterpreterMethod> initMethod(AbstractClass<?> caller, String name, AbstractClass<?> type, Token params, Token body, AbstractFrame declared) throws InterpreterException {
    name = name + ":{";
    ArrayList<Parameter> parameters = new ArrayList<>();
    String typeName = type.getName() + ":{" + params.toString() + "}";
    Token par = params;
    while (!params.isNull()) {
      String p = (String) params.getCar();
      while ((params = params.getNextToken()).equals(Interpreter.callType))
        p = p + ((String) ((Variable<?>) params.getCar()).getSource()) + ((Variable<?>) (params = params.getNextToken()).getCar()).getSource();
      name = name + p + ", ";
View Full Code Here

   */
  @Override
  public Token eval(AbstractClass<?> caller, Token parameters, AbstractFrame frame) throws InterpreterException {
    ArrayList<Token> args = new ArrayList<>();
    for (int i = 0; i < this.getParameters().length && !parameters.isNull(); i++) {
      Token arg = parameters.singular(), head = arg;
      while (!(parameters = parameters.getNextToken()).getCarType().equals(Interpreter.separatorType) && !parameters.isNull())
        head = head.append(parameters.singular());
      Token value = frame.getInterpreter().eval(caller, arg, frame);
      //TODO add type length checking, var args
      if (((AbstractObject<?>) value.getCar()).getType().extend(this.getParameters()[i].getType()) == -1)
        throw new UnexpectedTypeException(((AbstractObject<?>) value.getCar()).getType().getName() + " cannot be cast to " + this.getParameters()[i].getType().getName());
      args.add(arg);
      parameters = parameters.getNextToken();
    }
    return eval(args);
  }
View Full Code Here

  @Override
  public Token eval(ArrayList<Token> args) throws ArrayIndexOutOfBoundsException, IllegalArgumentException, InterpreterException {
    Frame base = new Frame(getDeclared(), getDeclared().getInterpreter());
    for (int i = 0; i < args.size(); i++)
      base.writeVariable(this.getParameters()[i].getName(), args.get(i), true);
    Token result = getDeclared().getInterpreter().eval(container, getSource(), base);
    AbstractClass<?> returnType = ((AbstractMethodClass) getType()).getReturnType();
    if (returnType.equals(Interpreter.voidClass) && !result.isNull())
      throw new UnexpectedTypeException("Cannot return a value from a void function.");
    if (((Variable<?>) (result = result.getNextToken()).getCar()).getType().extend(returnType) == -1)
      if (Interpreter.isPrimitive(getType()) && Interpreter.isPrimitive(((Variable<?>) result.getCar()).getType()))
        return ((PrimitiveClass<?>) returnType).eval(((PrimitiveClass<?>) ((Variable<?>) result.getCar()).getType()).cast(((Variable<?>) result.getCar()).getSource(), (PrimitiveClass<?>) returnType));
      else
        throw new UnexpectedTypeException("Cannot return a " + ((Variable<?>) result.getCar()).getType() + " from a function that should return a " + getType());
    return result;
  }
View Full Code Here

      throw new UnendedLineException("An object declaration must have a body.");
    source = (Token) source.getCar();
    ArrayList<Token> lines = Interpreter.splitLines(source);
    ArrayList<Pair<Access, Token>> staticObjects = new ArrayList<>(), staticMethods = new ArrayList<>(), staticVariables = new ArrayList<>();
    while (lines.size() > 0) {
      Token line = lines.remove(0);
      Access access = Access.DEFAULT;
      boolean use = false;
      while (!line.isNull()) {
        if (line.getCarType().equals(interpreter.importType)) {
          use = true;
          access = Access.PUBLIC;
          break;
        }
        else if (line.getCarType().equals(Interpreter.scopeType)) {
          access = Access.getAccess((String) line.getCar());
        }
        else if (line.getCarType().equals(Interpreter.staticType))
          use = true;
        else
          break;
        line = line.getNextToken();
      }
      //object [name] (extends <type>)? {body}
      if (line.getCarType().equals(Interpreter.classType)) {
        if (use) {
          staticObjects.add(new Pair<>(access, line));
          String nme;
          frames[access.intValue()].addType((nme = (String) (line = line.getNextToken()).getCar()),
              new InterpreterClass(nme, (line = line.getNextToken()).getNextToken(), frames[3], new Type<InterpreterClass>(nme)));
        }
        else
          objects.add(new Pair<>(access, line));
      }
      else {
        //Constructor
        if (!use && line.getCar().equals(getName()) && line.getNextToken().getCarType().equals(Interpreter.parenthesesType))
          methods.add(new Pair<>(access, line));
        //Function
        else if (line.getCarType().equals(Interpreter.identifierType) && line.getNextToken().getCarType().equals(Interpreter.identifierType) &&
            line.getNextToken().getNextToken().getCarType().equals(Interpreter.parenthesesType))
          if (use)
            staticMethods.add(new Pair<>(access, line));
          else
            methods.add(new Pair<>(access, line));
        //Variables
        else if (use)
          staticVariables.add(new Pair<>(access, line));
        else
          variables.add(new Pair<>(access, line));
      }
    }
    construct(staticObjects, staticMethods, staticVariables, frames);
    frames[0].writeVariable("class", new Token(this, getTokenType()), true); //Adds .class
  }
View Full Code Here

  }
 
  protected void construct(ArrayList<Pair<Access, Token>> objects, ArrayList<Pair<Access, Token>> methods,
      ArrayList<Pair<Access, Token>> variables, Frame[] frames) throws InterpreterException {
    for (Pair<Access, Token> p : objects) {
      Token line = p.getY();
      String nme;
      frames[p.getX().intValue()].addType((nme = (String) (line = line.getNextToken()).getCar()),
          new InterpreterClass(nme, (line = line.getNextToken()).getNextToken(), frames[3], new Type<InterpreterClass>(nme)));;
    }
    for (Pair<Access, Token> p : methods) {
      Pair<String, InterpreterMethod> method = InterpreterMethod.initMethod(this, p.getY(), frames[3]);
      if (method.getX().indexOf(':') == 0) //This is a constructor
        frames[p.getX().intValue()].writeVariable(getName() + method.getX(), new Token(InterpreterConstructor.init(method.getY()),
            method.getY().getType().getTokenType()), true);
      frames[p.getX().intValue()].writeVariable(method.getX(), new Token(method.getY(), method.getY().getType().getTokenType()), true);
    }
    for (Pair<Access, Token> p : variables)
      Interpreter.evalVariableDeclaration(this, p.getY(), frames[3], frames[p.getX().intValue()]);
  }
View Full Code Here

    String name = getName().substring(getName().lastIndexOf('.') + 1); //This works because it returns -1 if there is no match, and -1 + 1 = 0 = complete String
    ArrayList<Token> arguments = Interpreter.evalFunctionArguments(caller, parameters, frame);
    name = Interpreter.evalFunctionName(name, arguments);
    InterpreterConstructor constructor = (InterpreterConstructor) frames[access.intValue()].readVariable(name).getCar();
    AbstractObject<?> superObject = constructor.initSuperclass(caller, superclass, parameters, frame);
    Token result = new Token(new InterpreterObject(this, superObject, frames), getTokenType());
    //Initialize keywords
    frames[3].writeVariable("this", result, true);
    frames[3].writeVariable("super", new Token(superObject, superclass.getTokenType()), true);
    constructor.eval(arguments);
    return result;
  }
View Full Code Here

    return new InterpreterClass(getName(), getSource(), getTokenType(), superclass, frames, objects, methods, variables);
  }

  @Override
  public Token makePlaceholder() {
    return new Token(new InterpreterObject(this, null, null), getTokenType());
  }
View Full Code Here

    }
    throw new VariableNotFoundException("No methods that match " + name + "} exist in objects with the type, " + getSource().getName());
  }
 
  public Token eval(Object source) {
    return new Token(new JavaObject<>(this, (T) source), getTokenType());
  }
View Full Code Here

      if (matches) {
        Object[] p2 = new Object[p.length];
        for (int i = 0; i < params.size(); i++)
          p2[i] = ((Variable<?>) params.get(i).getCar()).getSource();
        try {
          return new Token(new JavaObject<T>(this, (T) constructor.newInstance(p2)), getTokenType());
        }
        catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
          e.printStackTrace();
        }
      }
View Full Code Here

TOP

Related Classes of scriptingLanguage.Token

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.