Example #1
0
            static LitType()
            {
                stTypes = new Dictionary<string, LitType>();

                LitType tword = new LitType( "u16", 1, null,
                    delegate( String str )
                    {
                        int val = 0;
                        Match match;
                        if ( ( match = Regex.Match( str, "(0d)?[0-9]+" ) ).Success
                            && match.Index == 0 )
                        {
                            String sub = match.Value;
                            if ( sub.Length > 2 && sub[ 1 ] == 'd' )
                                sub = sub.Substring( 2 );

                            val = int.Parse( sub );
                        }
                        else if ( ( match = Regex.Match( str, "0b(0|1)+" ) ).Success
                            && match.Index == 0 )
                        {
                            val = int.Parse( match.Value );
                        }
                        else if ( ( match = Regex.Match( str, "0x[0-9a-f]+" ) ).Success
                            && match.Index == 0 )
                        {
                            val = int.Parse( match.Value );
                        }
                        else
                            return -1;

                        if ( val < 0x10000 )
                            return match.Length;

                        return -1;
                    },
                    delegate( String literal )
                    {
                        if( literal.StartsWith( "'" ) )
                        {
                            char c = literal[ 1 ];
                            if( c == '\\' )
                            {
                                c = literal[ 2 ];
                                switch( c )
                                {
                                    case 'r':
                                        c = '\r'; break;
                                    case 'n':
                                        c = '\n'; break;
                                    case 't':
                                        c = '\t'; break;
                                }
                            }
                            return new ushort[] { (ushort) c };
                        }

                        if( literal.Length > 2 && char.IsLetter( literal[ 1 ] ) )
                        {
                            int b = 10;
                            switch( literal[ 1 ] )
                            {
                                case 'b':
                                    b = 2; break;
                                case 'd':
                                    b = 10; break;
                                case 'x':
                                    b = 16; break;
                            }
                            return new ushort[] { Convert.ToUInt16( literal.Substring( 2 ), b ) };
                        }

                        return new ushort[] { ushort.Parse( literal ) };
                    } );
                stTypes.Add( tword.Identifier, tword );
                LitType tbool = new LitType( "bool", 1, null,
                    delegate( String str )
                    {
                        Match match;
                        if ( ( match = Regex.Match( str, "true[^a-zA-Z0-9_]" ) ).Success
                            && match.Index == 0 )
                            return 4;
                        else if ( ( match = Regex.Match( str, "false[^a-zA-Z0-9_]" ) ).Success
                            && match.Index == 0 )
                            return 5;

                        return -1;
                    },
                    delegate( String literal )
                    {
                        if ( literal == "true" )
                            return new ushort[] { 1 };
                        return new ushort[] { 0 };
                    } );
                stTypes.Add( tbool.Identifier, tbool );

                tword.AddMethod( "__ctor", new ValType[] { tword }, tword, true, "" );
                tword.AddMethod( "__ctor", new ValType[] { tbool }, tword, true, "" );
                tword.AddMethod( "__not", new ValType[] { tword }, tword, true, @"
                    XOR A, 0xffff
                " );
                tword.AddMethod( "__add", new ValType[] { tword, tword }, tword, true, @"
                    ADD A, B
                " );
                tword.AddMethod( "__sub", new ValType[] { tword, tword }, tword, true, @"
                    SUB A, B
                " );
                tword.AddMethod( "__mul", new ValType[] { tword, tword }, tword, true, @"
                    MUL A, B
                " );
                tword.AddMethod( "__div", new ValType[] { tword, tword }, tword, true, @"
                    DIV A, B
                " );
                tword.AddMethod( "__mod", new ValType[] { tword, tword }, tword, true, @"
                    MOD A, B
                " );
                tword.AddMethod( "__shl", new ValType[] { tword, tword }, tword, true, @"
                    SHL A, B
                " );
                tword.AddMethod( "__shr", new ValType[] { tword, tword }, tword, true, @"
                    SHR A, B
                " );
                tword.AddMethod( "__and", new ValType[] { tword, tword }, tword, true, @"
                    AND A, B
                " );
                tword.AddMethod( "__bor", new ValType[] { tword, tword }, tword, true, @"
                    BOR A, B
                " );
                tword.AddMethod( "__xor", new ValType[] { tword, tword }, tword, true, @"
                    XOR A, B
                " );
                tword.AddMethod( "__equ", new ValType[] { tword, tword }, tbool, false, @"
                    SET C, A
                    SET A, 0
                    IFE B, C
                        SET A, 1
                    SET PC, POP
                " );
                tword.AddMethod( "__neq", new ValType[] { tword, tword }, tbool, false, @"
                    SET C, A
                    SET A, 0
                    IFN B, C
                        SET A, 1
                    SET PC, POP
                " );
                tword.AddMethod( "__grt", new ValType[] { tword, tword }, tbool, false, @"
                    SET C, A
                    SET A, 0
                    IFG B, C
                        SET A, 1
                    SET PC, POP
                " );
                tword.AddMethod( "__gre", new ValType[] { tword, tword }, tbool, false, @"
                    SET C, A
                    SET A, 1
                    IFG C, B
                        SET A, 0
                    SET PC, POP
                " );
                tword.AddMethod( "__lst", new ValType[] { tword, tword }, tbool, false, @"
                    SET C, A
                    SET A, 0
                    IFG C, B
                        SET A, 1
                    SET PC, POP
                " );
                tword.AddMethod( "__lse", new ValType[] { tword, tword }, tbool, false, @"
                    SET C, A
                    SET A, 1
                    IFG B, C
                        SET A, 0
                    SET PC, POP
                " );

                tbool.AddMethod( "__ctor", new ValType[] { tbool }, tbool, true, "" );
                tbool.AddMethod( "__ctor", new ValType[] { tword }, tbool, false, @"
                    SET B, A
                    SET A, 0
                    IFG B, 0
                        SET A, 1
                    SET PC, POP
                " );
                tbool.AddMethod( "__not", new ValType[] { tbool }, tbool, true, @"
                    XOR A, 1
                " );
                tbool.AddMethod( "__and", new ValType[] { tbool, tbool }, tbool, true, @"
                    AND A, B
                " );
                tbool.AddMethod( "__bor", new ValType[] { tbool, tbool }, tbool, true, @"
                    BOR A, B
                " );
                tbool.AddMethod( "__xor", new ValType[] { tbool, tbool }, tbool, true, @"
                    XOR A, B
                " );
                tbool.AddMethod( "__equ", new ValType[] { tbool, tbool }, tbool, false, @"
                    SET C, A
                    SET A, 0
                    IFE B, C
                        SET A, 1
                    SET PC, POP
                " );
                tbool.AddMethod( "__neq", new ValType[] { tbool, tbool }, tbool, false, @"
                    SET C, A
                    SET A, 0
                    IFN B, C
                        SET A, 1
                    SET PC, POP
                " );
            }
Example #2
0
 public Literal(ParseTree.Node?parseTreeNode, LitType type, string value)
     : base(parseTreeNode)
 {
     Type  = type;
     Value = value;
 }