<commit_before>// Copyright 2008 <NAME> <<EMAIL>>
//
// This software may be used and distributed according to the terms
// of the Genyris License, in the file "LICENSE", incorporated herein by reference.
//
package org.genyris.interp;

import java.util.HashMap;

import org.genyris.core.Exp;
import org.genyris.core.Lcons;
import org.genyris.core.Lsymbol;
import org.genyris.exception.AccessException;
import org.genyris.exception.GenyrisException;

public class MagicEnvironment extends StandardEnvironment {
    // This environment encompasses an expression (Exp) which provides
    // the first place to look for slots.
    // TODO - rename this class, perhaps.
    private Exp _it;

    public MagicEnvironment(Environment runtime, Exp theObject) throws GenyrisException {
        super(runtime, new HashMap());
        _it = theObject;
    }

    public Exp lookupVariableShallow(Exp symbol) throws UnboundException {
        if (symbol == _classes) {
            return _it.getClasses(_parent);
        }
        else if (symbol == _self) {
            return _it;
        }
        // TODO - DRY
        // TODO - move these into the Lcons class as an Environment
        else if (symbol == _left) {
            try {
                return _it.car();
            }
            catch (AccessException e) {
                throw new UnboundException(e.getMessage());
            }
        }
        else if (symbol == _right) {
            try {
                return _it.cdr();
            }
            catch (AccessException e) {
                throw new UnboundException(e.getMessage());
            }
        }
        throw new UnboundException("unbound symbol " + symbol.toString());
    }

    public Exp lookupInThisClassAndSuperClasses(Exp symbol) throws UnboundException {
        throw new UnboundException("unbound symbol " + symbol.toString());
    }

    private Exp lookupInClasses(Exp symbol) throws UnboundException {
        Exp classes = _it.getClasses(_parent);
        while (classes != NIL) {
            try {
                Environment klass = (Environment) (classes.car());
                try {
                    return (Exp) klass.lookupInThisClassAndSuperClasses(symbol);
                }
                catch (UnboundException e) {
                    ;
                }
                finally {
                    classes = classes.cdr();
                }
            }
            catch (AccessException e) {
                throw new UnboundException("bad classes list in object");
            }
        }
        throw new UnboundException("unbound symbol: " + symbol.toString());
    }

    public Exp lookupVariableValue(Exp exp) throws UnboundException {
        Exp sym = exp;
        boolean isMember = false;
        if (exp.listp()) {
            Lcons tmp = (Lcons) exp;
            if (tmp.car() == _dynamic) {
                tmp = (Lcons) tmp.cdr(); // TODO unsafe downcast
                sym = tmp.car();
                isMember = true;
            }
            else {
                throw new UnboundException("cannot set to a bad place" + exp.toString());
            }
        }
        Lsymbol symbol = (Lsymbol) exp;
        if (isMember) {
            return lookupDynamicVariableValue(sym);
        }
        else
            return super.lookupVariableValue(symbol);
    }

    public void defineVariable(Exp symbol, Exp valu) throws GenyrisException {
        Exp sym = symbol;
        boolean isMember = false;
        if (symbol.listp()) {
            Lcons tmp = (Lcons) symbol;
            if (tmp.car() == _dynamic) {
                tmp = (Lcons) tmp.cdr(); // TODO unsafe downcast
                sym = tmp.car();
                isMember = true;
            }
            else {
                throw new UnboundException("cannot set to a bad place" + symbol.toString());
            }
        }
        if (!(sym instanceof Lsymbol)) {
            throw new GenyrisException("cannot define non-symbol: " + symbol.toString());
        }<commit_msg>Fixed defect in lookup of dynamics.<commit_after>// Copyright 2008 <NAME> <<EMAIL>>
//
// This software may be used and distributed according to the terms
// of the Genyris License, in the file "LICENSE", incorporated herein by reference.
//
package org.genyris.interp;

import java.util.HashMap;

import org.genyris.core.Exp;
import org.genyris.core.Lcons;
import org.genyris.core.Lsymbol;
import org.genyris.exception.AccessException;
import org.genyris.exception.GenyrisException;

public class MagicEnvironment extends StandardEnvironment {
    // This environment encompasses an expression (Exp) which provides
    // the first place to look for slots.
    // TODO - rename this class, perhaps.
    private Exp _it;

    public MagicEnvironment(Environment runtime, Exp theObject) throws GenyrisException {
        super(runtime, new HashMap());
        _it = theObject;
    }

    public Exp lookupVariableShallow(Exp symbol) throws UnboundException {
        if (symbol == _classes) {
            return _it.getClasses(_parent);
        }
        else if (symbol == _self) {
            return _it;
        }
        // TODO - DRY
        // TODO - move these into the Lcons class as an Environment
        else if (symbol == _left) {
            try {
                return _it.car();
            }
            catch (AccessException e) {
                throw new UnboundException(e.getMessage());
            }
        }
        else if (symbol == _right) {
            try {
                return _it.cdr();
            }
            catch (AccessException e) {
                throw new UnboundException(e.getMessage());
            }
        }
        throw new UnboundException("unbound symbol " + symbol.toString());
    }

    public Exp lookupInThisClassAndSuperClasses(Exp symbol) throws UnboundException {
        throw new UnboundException("unbound symbol " + symbol.toString());
    }

    private Exp lookupInClasses(Exp symbol) throws UnboundException {
        Exp classes = _it.getClasses(_parent);
        while (classes != NIL) {
            try {
                Environment klass = (Environment) (classes.car());
                try {
                    return (Exp) klass.lookupInThisClassAndSuperClasses(symbol);
                }
                catch (UnboundException e) {
                    ;
                }
                finally {
                    classes = classes.cdr();
                }
            }
            catch (AccessException e) {
                throw new UnboundException("bad classes list in object");
            }
        }
        throw new UnboundException("unbound symbol: " + symbol.toString());
    }

    public Exp lookupVariableValue(Exp exp) throws UnboundException {
        Exp sym = exp;
        boolean isMember = false;
        if (exp.listp()) {
            Lcons tmp = (Lcons) exp;
            if (tmp.car() == _dynamic) {
                tmp = (Lcons) tmp.cdr(); // TODO unsafe downcast
                sym = tmp.car();
                isMember = true;
            }
            else {
                throw new UnboundException("bad dynamic: " + exp.toString());
            }
        }
        Lsymbol symbol = (Lsymbol) sym;
        if (isMember) {
            return lookupDynamicVariableValue(sym);
        }
        else
            return super.lookupVariableValue(symbol);
    }

    public void defineVariable(Exp symbol, Exp valu) throws GenyrisException {
        Exp sym = symbol;
        boolean isMember = false;
        if (symbol.listp()) {
            Lcons tmp = (Lcons) symbol;
            if (tmp.car() == _dynamic) {
                tmp = (Lcons) tmp.cdr(); // TODO unsafe downcast
                sym = tmp.car();
                isMember = true;
            }
            else {
                throw new UnboundException("cannot set to a bad place" + symbol.toString());
            }
        }
        if (!(sym instanceof Lsymbol)) {
            throw new GenyrisException("cannot define non-symbol: " + symbol.toString());
        }
{'fraction_non_alphanumeric': 0.09385834571570084, 'fraction_numerical': 0.0009905894006934125, 'mean_word_length': 1.6762756792577866, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4254403', 'n_tokens_mistral': 2084, 'n_tokens_neox': 2015, 'n_words': 601}