Package abstrasy.pcfx

Source Code of abstrasy.pcfx.PCFx_include

package abstrasy.pcfx;


import abstrasy.ASymbol;
import abstrasy.Heap;
import abstrasy.Interpreter;
import abstrasy.Node;
import abstrasy.SourceFile;

import abstrasy.interpreter.InterpreterException;
import abstrasy.interpreter.StdErrors;

import java.io.File;

import java.net.URL;


/**
* Abstrasy Interpreter
*
* Copyright : Copyright (c) 2006-2012, Luc Bruninx.
*
* Concédée sous licence EUPL, version 1.1 uniquement (la «Licence»).
*
* Vous ne pouvez utiliser la présente oeuvre que conformément à la Licence.
* Vous pouvez obtenir une copie de la Licence à l’adresse suivante:
*
*   http://www.osor.eu/eupl
*
* Sauf obligation légale ou contractuelle écrite, le logiciel distribué sous
* la Licence est distribué "en l’état", SANS GARANTIES OU CONDITIONS QUELLES
* QU’ELLES SOIENT, expresses ou implicites.
*
* Consultez la Licence pour les autorisations et les restrictions
* linguistiques spécifiques relevant de la Licence.
*
*
* @author Luc Bruninx
* @version 1.0
*/

public class PCFx_include extends PCFx {

    public PCFx_include() {
    }

    /**
     * eval
     *
     * @param startAt Node
     * @return Node
     * @throws Exception
     * @todo Implémenter cette méthode abstrasy.PCFx
     */

    public Node eval(Node startAt) throws Exception {
        String npath = null;

        switch (startAt.size()) {
            case 2:
                /**
                 * forme (include string) -> resultat
                 */
                npath = startAt.getSubNode(1, Node.TYPE_STRING).getString();
                break;
            default:
                throw new InterpreterException(StdErrors.Argument_count_mismatch);
        }


        File src = SourceFile.findSourceFileName(npath);
        String msrc = "";
        SourceFile in = null;

        if (src != null) {
            in = new SourceFile(src.getAbsolutePath());
        }
        else {
            Node iPaths = Heap.getv(ASymbol.SYMBOL_IMPORTS_PATH);
            if (iPaths != null) {
                if (iPaths.getQType()==Node.TYPE_CLIST) {
                    for (int ip = (iPaths.size() - 1); ip >= 0; ip--) {
                        Node pval = iPaths.elementAt(ip);
                        if (pval.isString()) {
                            src = SourceFile.findSourceFileName(pval.getString() + npath);
                            if (src != null) {
                                in = new SourceFile(src.getAbsolutePath());
                                ip = -1;
                            }
                        }
                    }
                }
            }
        }
        if (in == null) {
            Node iPaths = Heap.getv(ASymbol.SYMBOL_IMPORTS_BASE_URL);
            if (iPaths != null) {
                if (iPaths.getQType()==Node.TYPE_CLIST) {
                    for (int ip = (iPaths.size() - 1); ip >= 0; ip--) {
                        Node pval = iPaths.elementAt(ip);
                        if (pval.isString()) {
                            URL surl = SourceFile.findSourceURLName(pval.getString() + npath);
                            if (surl != null) {
                                in = new SourceFile(surl);
                                ip = -1;
                            }
                        }
                    }
                }
            }

        }
        if (in != null) {
            try {
                in.load();
                msrc = in.getSource();
                Interpreter interpreter = Interpreter.interpr_getNewChildInterpreter();
                //Register continue dans le même thread...
                Heap.push(); // mais en créant une closure
                interpreter.setSource(msrc);
                interpreter.compile();
                Node res = interpreter.getNode().exec(false);
                Heap.pull();

                return res;

            }
            catch (Exception ex) {
                if (Interpreter.isDebugMode()) {
                    ex.printStackTrace();
                }
                throw new InterpreterException(StdErrors.createTrace(startAt, ex));
            }

        }
        else {
            throw new InterpreterException(StdErrors.Cannot_import_module);
        }

    }

}
TOP

Related Classes of abstrasy.pcfx.PCFx_include

TOP
Copyright © 2018 www.massapi.com. 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.