Exemple #1
0
        public Primary VisitLisp(Lisp basetype)
        {
            Primary result;

            if (basetype.Primaries?.Count == 0)
            {
                return(new Null());
            }

            if (basetype.Function != null)
            {
                return(basetype.Function.Accept(this));
            }
            else if (basetype.Primaries[0] is Identifier i && currentContext.AstList.FirstOrDefault(n => n.Key.Equals(i.Value)).Value is Function f)
            {
                // Interprete primaries before call with args (unless if)
                if (f.Identifier != "if")
                {
                    List <Primary> args    = new List <Primary>();
                    List <Primary> oldargs = basetype.Primaries.GetRange(1, basetype.Primaries.Count() - 1);
                    foreach (Primary p in oldargs)
                    {
                        args.Add(p.Accept(this));
                    }
                    return(f.AcceptCall(this, args.ToArray()));
                }
                return(f.AcceptCall(this, basetype.Primaries.GetRange(1, basetype.Primaries.Count() - 1).ToArray()));
            }
Exemple #2
0
        private Function CreateFunction()
        {
            List <Pattern> patterns = new List <Pattern>();

            if (PeekToken().TokenType != TokenType.IDENTIFIER)
            {
                throw new CompilerException("Need identifier to create function");
            }

            string Ident = NextToken().Value;

            while (PeekToken().TokenType != TokenType.RIGHT_PAREN)
            {
                if (PeekToken().TokenType != TokenType.LEFT_BRACKET)
                {
                    throw new CompilerException("Need at least 1 pattern to create function");
                }

                NextToken(); // Skip left bracket
                Matcher matcher = CreateMatcher();
                NextToken(); // Skip right bracket
                if (PeekToken().TokenType != TokenType.LEFT_PAREN)
                {
                    throw new CompilerException("Need Lisp after matcher");
                }

                Lisp result = CreateLisp();

                patterns.Add(new Pattern(matcher, result));
            }

            NextToken(); // Skip right parentheses

            return(new Function(Ident, patterns));
        }
Exemple #3
0
        public string VisitLisp(Lisp basetype)
        {
            if (basetype.Function != null)
            {
                return(Parenthesize("Lisp fun", basetype.Function));
            }
            else
            {
                string returnString = "";
                foreach (Primary p in basetype.Primaries)
                {
                    returnString += Parenthesize("Primary", p);
                }

                return(returnString);
            }
        }
Exemple #4
0
 public Pattern(Matcher matcher, Lisp result)
 {
     Matcher = matcher;
     Result  = result;
 }