<commit_before>#!/usr/bin/env python
#
# The language interpreter for lcad.
#
# Hazen 07/14
#

import copy
import numpy

import lcadExceptions as lce
import functions
import lexerParser


class LEnv(object):
    """
    This keeps track of the current lexical environment.
    """
    def __init__(self, debug = False, add_builtins = True):
        self.debug = debug

        self.symbols = {}
        if add_builtins:
            for fn_name in functions.builtin_functions.keys():
                self.symbols[fn_name] = Symbol(fn_name)
                self.symbols[fn_name].setv(functions.builtin_functions[fn_name])

    def makeCopy(self):
        a_copy = LEnv(add_builtins = False)
        a_copy.debug = self.debug
        a_copy.symbols = self.symbols.copy()
        return a_copy

class Model(object):
    """
    This keeps track of the current "model", i.e. the 
    transformation matrix and the parts list.
    """
    def __init__(self, debug = False):
        self.m = numpy.identity(4)
        self.parts_list = []

    def makeCopy(self):
        a_copy = Model()
        a_copy.m = self.m.copy()
        a_copy.parts_list = self.parts_list
        return a_copy

class Symbol(object):
    """
    Box symbols so that they don't get copied when the environment gets copied.
    """
    def __init__(self, name):
        self.is_set = False
        self.name = name
        self.used = False
        self.value = None

    def getv(self, env):
        if not self.is_set:
            raise lce.VariableNotSetException(env, self.name)
        self.used = True
        return self.value

    def setv(self, value):
        self.is_set = True
        self.value = value


def dispatch(func, model, tree):
    """
    This handles function calls to both user-defined and built-in functions.
    """
    func.argCheck(tree)
    return func.call(model, tree)

def createLexicalEnv(lenv, tree):
    """
    Recursively walk the AST to determine the lexical environment by creating
    variables and functions, along with environment in which the function
    will be called. 

    Expressions and Symbols in the tree are then decorated with this lexical environments.
    """

    if isinstance(tree, lexerParser.LCadExpression):
        tree.lenv = lenv
        flist = tree.value

        # Empty list.
        if (len(flist) == 0):
            return

        # First element is a symbol, have to handle this specially in
        # case the symbol is "def".
        start = 0
        if isinstance(flist[0], lexerParser.LCadSymbol):
            start = 1
            flist[0].lenv = tree.lenv

            # First element is def.
            if (flist[0].value == "def"):
                start = len(flist)
            
                # def needs at least 3 arguments.
                if (len(flist)<3):
                    raise lce.NumberArgumentsException(tree, "2 or more", len(flist) - 1)

                # 4 arguments means this is a function definition.
                if (len(flist)==4):
                    tree.lenv.symbols[flist[1].value] = Symbol(flist[i].value)
                    tree.lenv.symbols[flist[1].value].setv(functions.UserFunction(tree.lenv.makeCopy(), tree))

                # Otherwise it defines one (or more variables).<commit_msg>Fix typo.<commit_after>#!/usr/bin/env python
#
# The language interpreter for lcad.
#
# Hazen 07/14
#

import copy
import numpy

import lcadExceptions as lce
import functions
import lexerParser


class LEnv(object):
    """
    This keeps track of the current lexical environment.
    """
    def __init__(self, debug = False, add_builtins = True):
        self.debug = debug

        self.symbols = {}
        if add_builtins:
            for fn_name in functions.builtin_functions.keys():
                self.symbols[fn_name] = Symbol(fn_name)
                self.symbols[fn_name].setv(functions.builtin_functions[fn_name])

    def makeCopy(self):
        a_copy = LEnv(add_builtins = False)
        a_copy.debug = self.debug
        a_copy.symbols = self.symbols.copy()
        return a_copy

class Model(object):
    """
    This keeps track of the current "model", i.e. the 
    transformation matrix and the parts list.
    """
    def __init__(self, debug = False):
        self.m = numpy.identity(4)
        self.parts_list = []

    def makeCopy(self):
        a_copy = Model()
        a_copy.m = self.m.copy()
        a_copy.parts_list = self.parts_list
        return a_copy

class Symbol(object):
    """
    Box symbols so that they don't get copied when the environment gets copied.
    """
    def __init__(self, name):
        self.is_set = False
        self.name = name
        self.used = False
        self.value = None

    def getv(self, env):
        if not self.is_set:
            raise lce.VariableNotSetException(env, self.name)
        self.used = True
        return self.value

    def setv(self, value):
        self.is_set = True
        self.value = value


def dispatch(func, model, tree):
    """
    This handles function calls to both user-defined and built-in functions.
    """
    func.argCheck(tree)
    return func.call(model, tree)

def createLexicalEnv(lenv, tree):
    """
    Recursively walk the AST to determine the lexical environment by creating
    variables and functions, along with environment in which the function
    will be called. 

    Expressions and Symbols in the tree are then decorated with this lexical environments.
    """

    if isinstance(tree, lexerParser.LCadExpression):
        tree.lenv = lenv
        flist = tree.value

        # Empty list.
        if (len(flist) == 0):
            return

        # First element is a symbol, have to handle this specially in
        # case the symbol is "def".
        start = 0
        if isinstance(flist[0], lexerParser.LCadSymbol):
            start = 1
            flist[0].lenv = tree.lenv

            # First element is def.
            if (flist[0].value == "def"):
                start = len(flist)
            
                # def needs at least 3 arguments.
                if (len(flist)<3):
                    raise lce.NumberArgumentsException(tree, "2 or more", len(flist) - 1)

                # 4 arguments means this is a function definition.
                if (len(flist)==4):
                    tree.lenv.symbols[flist[1].value] = Symbol(flist[1].value)
                    tree.lenv.symbols[flist[1].value].setv(functions.UserFunction(tree.lenv.makeCopy(), tree))

                # Otherwise it defines one (or more variables).
{'fraction_non_alphanumeric': 0.09569597069597069, 'fraction_numerical': 0.005952380952380952, 'mean_word_length': 2.0436600092893635, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '467512', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1884, 'n_words': 562}