User:Kiken/Construction Site/Implementations/Sōap

From Esolang
Jump to navigation Jump to search
import re

class SooapError(SyntaxError):
    pass

def sooap(code):
    functions = {}
    vars = {}
    result = ''
    lineno = 0
    code = code.split('\n')
    for line in code:
        if code[line][len(code[line])] != ':'
            raise SooapError('expected \':\'')
        if re.sub('\s', '', code[line+1]) == '':
            raise SooapError('expected description')
        if re.sub('\s', '', code[line+2]) == '':
            raise SooapError('description must end with a blank line')
        if line > 2:
            if line == 3:
                if code[line][len(code[line])] != ':'
                    raise SooapError('expected \':\'')
                if code[line] != 'Ingredients:'
                    raise SooapError('expected title \'Ingredients\'')
            if line == 4:
                if re.sub('\s', '', code[line]) == '':
                    raise SooapError('expected list of ingredients')
                else:
                    temp = code[line].split(', ')
                    if not temp.endswith('.'):
                        raise SooapError('list of ingredients must end with a period)
                    for ingredients in temp:
                        if not ingredients[0].isupper():
                            raise SooapError('ingredient names must begin with a capitalized letter')
                        else:
                            vars[ingredients] = 0
            if line == 5:
                if not code[line] == '':
                    raise SooapError('title declarations must end with a blank line')
            if line == 6:
                if code[line][len(code[line])] != ':'
                    raise SooapError('expected \':\'')
                if code[line] != 'Directions for use:'
                    raise SooapError('expected title \'Directions for use\'')
            if line == 7:
                endofsentence = False
                if not code[line].startswith('Turn the pump and press.'):
                    raise SooapError('script must begin with pressing the pump')
                else:
                    temp = re.split('[,.]', code[line].strip(' '))[len(code[line])]
                    temp1 = [code[line].strip(' ')[len(code[line].strip(' '))]]
                    for instruction in temp:
                        punc = temp1[temp.index(instruction)]
                        if endofsentence and instruction[0].islower():
                            raise SooapError('beginning of sentences must begin with a capitalized letter')
                        elif not endofsentence and instruction[0].isupper():
                            raise SooapError('middle of sentences mustn't begin with a capitalized letter')
                        else:
                            if instruction == 'Turn the pump and press':
                                continue
                            else:
                                search = re.search('[tT]he [A-Z][a-z]* will .* your hands clean', instruction)
                                if search:
                                    varName = re.findall('[A-Z][a-z]*', search.string.strip('The '))[0]
                                    action = re.search('wet|dry|rinse', search.string).group()
                                if varName in vars:
                                    if action:
                                        if action == 'wet':
                                            vars[varName] -= 1
                                        elif action == 'rinse':
                                            vars[varName] = 0
                                        else:
                                            vars[varName] += 1
                                    else:
                                        raise SooapError(f'invalid action \'{action}\'')
                                else:
                                    raise SooapError(f'no ingredient named \'{varName}\'')
                        if punc == '.':
                            endofsentence = True
                        else:
                            endofsentence = False
# working on it