//增发货币
        public static bool increaseByBu(string name, byte[] to, BigInteger value)
        {
            if (to.Length != 20)
            {
                return(false);
            }

            if (value <= 0)
            {
                return(false);
            }

            var key = getNameKey(name.AsByteArray());

            byte[] token = Storage.Get(Storage.CurrentContext, key);
            if (token.Length == 0)
            {
                return(false);
            }

            transfer(name, null, to, value);

            Tokenized t = Helper.Deserialize(token) as Tokenized;

            t.totalSupply = t.totalSupply + value;

            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(t));
            return(true);
        }
        //销毁货币
        public static bool destoryByBu(string name, byte[] from, BigInteger value)
        {
            if (from.Length != 20)
            {
                return(false);
            }

            if (value <= 0)
            {
                return(false);
            }

            var key = new byte[] { 0x12 }.Concat(name.AsByteArray());

            byte[] token = Storage.Get(Storage.CurrentContext, key);
            if (token.Length == 0)
            {
                return(false);
            }

            transfer(name, from, null, value);

            Tokenized t = Helper.Deserialize(token) as Tokenized;

            t.totalSupply = t.totalSupply - value;

            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(t));
            return(true);
        }
 static private Node TokenizeVariable(Tokenized token, ref TokenIterator iter)
 {
     if (token.value == "exit")
     {
         return(new Node(Items.EXIT, null));
     }
     else if (token.value == "if")
     {
         Node ifstmt = new Node(Items.IF_BRANCH, null);
         return(ifstmt);
     }
     else if (token.value == "else")
     {
         Tokenized next = iter.get(BasicTokens.SPECIAL);
         if (next.value != ":")
         {
             throw new System.Exception("Invalid else statement");
         }
         return(new Node(Items.ELSE_BRANCH, null));
     }
     else if (token.value == "endif")
     {
         return(null);
     }
     else
     {
         Tokenized next = iter.get(new BasicTokens[] { BasicTokens.SPECIAL, BasicTokens.VARIABLE });
         if (next.value == "=")
         {
             Tokenized rhs = iter.get(new BasicTokens[] { BasicTokens.COMMAND, BasicTokens.STRING, BasicTokens.BOOLEAN, BasicTokens.INTEGER, BasicTokens.VARIABLE });
             string    rhs_str;
             if (rhs.type == BasicTokens.STRING)
             {
                 rhs_str = ParseString(rhs, ref iter);
             }
             else
             {
                 rhs_str = rhs.value;
             }
             Node i = new Node(Items.ASSIGNMENT, new string[] { token.value, rhs_str, rhs.type.ToString() });
             return(i);
         }
         else if (next.value == "+")
         {
             Tokenized rhs = iter.get(new BasicTokens[] { BasicTokens.INTEGER, BasicTokens.VARIABLE });
             Node      i   = new Node(Items.INCREMENT, new string[] { token.value, rhs.value, rhs.type.ToString() });
             return(i);
         }
         else if (next.value == "-")
         {
             Tokenized rhs = iter.get(new BasicTokens[] { BasicTokens.INTEGER, BasicTokens.VARIABLE });
             Node      i   = new Node(Items.DECREMENT, new string[] { token.value, rhs.value, rhs.type.ToString() });
             return(i);
         }
         else
         {
             throw new System.Exception("Invalid characters " + next.value + " at line " + token.line.ToString());
         }
     }
 }
예제 #4
0
        public static bool destoryByBu(string name, byte[] from, BigInteger value)
        {
            if (from.Length != 20)
            {
                return(false);
            }

            if (value <= 0)
            {
                return(false);
            }

            var key = getNameKey(name.AsByteArray());

            byte[] token = Storage.Get(Storage.CurrentContext, key);
            if (token.Length == 0)
            {
                return(false);
            }

            if (!transfer(name, from, null, value))
            {
                throw new InvalidOperationException("Operation is error.");
            }

            Tokenized t = Helper.Deserialize(token) as Tokenized;

            t.totalSupply = t.totalSupply - value;

            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(t));
            return(true);
        }
 static private Node ParseBoolean(Tokenized lhs, ref TokenIterator iter, Tokenized terminate)
 {
     if (lhs.type == terminate.type && lhs.value == terminate.value)
     {
         return(null);
     }
     else if (lhs.type == BasicTokens.VARIABLE || lhs.type == BasicTokens.BOOLEAN || lhs.type == BasicTokens.INTEGER || lhs.type == BasicTokens.STRING)
     {
         Tokenized compare = iter.get(BasicTokens.SPECIAL);
         if (IsComparisonOperator(compare.value))
         {
             Tokenized rhs       = iter.get(new BasicTokens[] { BasicTokens.BOOLEAN, BasicTokens.INTEGER, BasicTokens.STRING, BasicTokens.VARIABLE });
             Node      condition = new Node(Items.CONDITION, new string[] { lhs.type.ToString(), lhs.value, compare.ToString(), rhs.type.ToString(), rhs.value });
             Node      andor     = ParseBoolean(iter.get(), ref iter, terminate);
             if (andor != null && andor.type != Items.COMPARISON)
             {
                 throw new System.Exception("Invalid boolean statement");
             }
             condition.firstChild = andor;
             return(condition);
         }
         else
         {
             throw new System.Exception("Found invalid token \"" + compare.value + "\" in boolean expression on line " + compare.line);
         }
     }
     else if (lhs.type == BasicTokens.SPECIAL)
     {
         Node compare, next;
         if (lhs.value == "&&")
         {
             compare = new Node(Items.COMPARISON, new string[] { "and" });
         }
         else if (lhs.value == "||")
         {
             compare = new Node(Items.COMPARISON, new string[] { "or" });
         }
         else
         {
             throw new System.Exception("Found invalid token " + lhs.value + " in boolean expression on line " + lhs.line);
         }
         compare = new Node(Items.COMPARISON, new string[] { "and" });
         next    = ParseBoolean(iter.get(), ref iter, terminate);
         if (next == null || next.type != Items.CONDITION)
         {
             throw new System.Exception("Invalid boolean expression");
         }
         compare.firstChild = next;
         return(compare);
     }
     else if (lhs.type != BasicTokens.OPEN_PAREN && lhs.type != BasicTokens.CLOSE_PAREN && lhs.type != BasicTokens.COMMAND)
     {
         return(ParseBoolean(iter.get(), ref iter, terminate));
     }
     else
     {
         throw new System.Exception("Invalid boolean statement");
     }
 }
    static private string MakeToken(BasicTokens t, string buf)
    {
        Tokenized nt = new Tokenized(t, buf);

        if (tokenList.Count != 0)
        {
            LastToken = tokenList[tokenList.Count - 1].type;
        }
        tokenList.Add(nt);
        return("");
    }
        public Tokenized get()
        {
            if (_index >= _tokens.Length)
            {
                return(_default);
            }
            Tokenized tok = _tokens[_index];

            _index++;
            return(tok);
        }
        public Tokenized get(BasicTokens expected)
        {
            if (_index >= _tokens.Length)
            {
                return(_default);
            }
            Tokenized tok = _tokens[_index];

            if (tok.type != expected)
            {
                throw new System.Exception("Unexpected token " + tok.value + " at line " + tok.line);
            }
            _index++;
            return(tok);
        }
    static private Node TokenizeString(Tokenized token, ref TokenIterator iter)
    {
        string    val  = ParseString(token, ref iter);
        Tokenized next = iter.get(new BasicTokens[] { BasicTokens.STRING, BasicTokens.NEW_LINE });

        if (next.type == BasicTokens.STRING)
        {
            string val2 = ParseString(next, ref iter);
            iter.get(BasicTokens.NEW_LINE);
            return(new Node(Items.DIALOGUE, new string[] { val, val2 }));
        }
        else
        {
            return(new Node(Items.CHOICE_LABEL, new string[] { val }));
        }
    }
        public static bool init(string name, string symbol, byte decimals)
        {
            var key = new byte[] { 0x12 }.Concat(name.AsByteArray());

            byte[] token = Storage.Get(Storage.CurrentContext, key);
            if (token.Length != 0)
            {
                return(false);
            }

            Tokenized t = new Tokenized();

            t.decimals    = decimals;
            t.name        = name;
            t.symbol      = symbol;
            t.totalSupply = 0;
            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(t));
            return(true);
        }
    static private Node TokenizeSpecial(Tokenized token, ref TokenIterator iter)
    {
        if (token.value == ".")
        {
            Tokenized next = iter.get(BasicTokens.STRING);

            Node i;
            if (next.value.Length == 0)
            {
                throw new System.Exception("Invalid state label \"" + next.value + "\" on line " + next.line + ", cannot have empty state label");
            }
            else if (next.value.Contains("%"))
            {
                throw new System.Exception("Invalid state label \"" + next.value + "\" on line " + next.line + ", cannot use variable insertion when defining a state label");
            }
            else if (stateLabels.TryGetValue(next.value, out i))
            {
                throw new System.Exception("Invalid state label \"" + next.value + "\" on line " + next.line + ", the state label is a duplicate");
            }
            iter.get(BasicTokens.NEW_LINE);

            i = new Node(Items.STATE_LABEL, new string[1] {
                next.value
            });
            stateLabels.Add(next.value, i);
            return(i);
        }
        else if (token.value == "=>")
        {
            Tokenized next = iter.get(new BasicTokens[] { BasicTokens.STRING, BasicTokens.VARIABLE });
            if (next.type == BasicTokens.VARIABLE && next.value != "exit")
            {
                throw new System.Exception("Invalid target \"" + next.value + "\" for jump");
            }
            iter.get(BasicTokens.NEW_LINE);
            Node i = new Node(Items.JUMPTO_LABEL, new string[] { next.value });
            return(i);
        }
        else
        {
            throw new System.Exception("Invalid characters " + token.value + " at line " + token.line.ToString());
        }
    }
        public Tokenized get(BasicTokens[] expected)
        {
            if (_index >= _tokens.Length)
            {
                return(_default);
            }
            Tokenized tok = _tokens[_index];
            bool      has = false;

            foreach (BasicTokens t in expected)
            {
                if (t == tok.type)
                {
                    has = true;
                    break;
                }
            }
            if (!has)
            {
                throw new System.Exception("Unexpected token " + tok.value + " at line " + tok.line);
            }
            _index++;
            return(tok);
        }
예제 #13
0
        public void BlahBlah()
        {
            var @in = new Readable <char>(
                "{\"Wibble\":123}".AsZeroTerminatedSpan()
                );

            var ops = new Readable <Op>(new[] {
                Op.ReadObject(),
                Op.DumpTo(0)
            });

            var tokenData = new Tokenized[16];
            var tokens    = new Buffer <Tokenized>(tokenData, 15);

            var charData = new char[16];
            var @out     = new Buffer <char>(charData, 15);

            var machine = new Machine();

            Write(ref tokens,
                  (0, 0, Token.Object),
                  (2, 6, Token.String),
                  (10, 3, Token.Number),
                  (13, 1, Token.ObjectEnd),
                  (0, 0, Token.End));

            while (true)
            {
                var(signal, bufferTag)
                    = machine.Next(ref ops, ref @in, ref tokens, ref @out);

                switch (signal)
                {
                case Signal.End:
                    return;

                case Signal.Underrun:
                    switch (bufferTag)
                    {
                    case Stream.Ops:
                        //load more ops here (should just be simple readable interface)
                        throw new NotImplementedException();

                    case Stream.In:
                        //read more chars into buffer and carry on
                        //and if we have to wait, then... we can wait at this top level
                        throw new NotImplementedException();

                    case Stream.Tokens:
                        //token underrun shouldn't be handled here, like
                        throw new NotImplementedException();

                    case Stream.Out1:
                        //this is the first output buffer
                        //how would this level cope with this? higher level contexts should bind to this
                        throw new NotImplementedException();

                    case Stream.Out2:
                        //second output buffer
                        throw new NotImplementedException();

                    default:
                        throw new NotImplementedException();
                    }
                }
            }
        }
    static public void Parse(TextAsset dialogue, ref DialogueDatabase db)
    {
        state      = new StateStack();
        lineNumber = 1;
        tokenList  = new List <Tokenized>();

        MakeToken(BasicTokens.NEW_LINE, "");

        string buffer = "";

        foreach (char c in dialogue.text)
        {
            //Debug.Log(buffer);

            if (state.value == State.COMMENT)
            {
                buffer = HandleComment(c, buffer);
            }

            else if (state.value == State.SPECIAL)
            {
                buffer = HandleSpecial(c, buffer);
            }

            else if (state.value == State.ESCAPE_CHAR)
            {
                buffer = HandleEscapeChar(c, buffer);
            }

            else if (state.value == State.COMMAND)
            {
                buffer = HandleCommand(c, buffer);
            }

            else if (state.value == State.STRING)
            {
                buffer = HandleString(c, buffer);
            }

            else if (state.value == State.DEFAULT)
            {
                buffer = HandleDefault(c, buffer);
            }

            if (c == '\n')
            {
                MakeToken(BasicTokens.NEW_LINE, "");
                lineNumber++;
            }
        }

        stateLabels = new Dictionary <string, Node>();

        int  ifelse_depth      = 0;
        int  indent_last_count = 0;
        int  indent_count      = 0;
        bool defining_choice   = false;

        Tokenized[]   tokens   = tokenList.ToArray();
        TokenIterator iterator = new TokenIterator(tokens);
        ItemChain     items    = new ItemChain();

        while (iterator.good())
        {
            Tokenized token = iterator.get();
            Node      i;
            switch (token.type)
            {
            case BasicTokens.NOT_ASSIGNED:
                throw new System.Exception("Unexpected file termination");

            case BasicTokens.NEW_LINE:
                indent_last_count = indent_count;
                indent_count      = 0;
                break;

            case BasicTokens.INDENT:
                ++indent_count;
                break;

            case BasicTokens.SPECIAL:
                i = TokenizeSpecial(token, ref iterator);
                if (i.type == Items.STATE_LABEL && defining_choice)
                {
                    defining_choice = false;
                    items.pop();
                    items.pop();
                }
                items.add(i);
                break;

            case BasicTokens.VARIABLE:
                i = TokenizeVariable(token, ref iterator);
                if (i == null)
                {
                    if (ifelse_depth == 0)
                    {
                        throw new System.Exception("Invalid endif statement");
                    }
                    for (int j = 0; j < ifelse_depth; j++)
                    {
                        items.pop();
                    }
                    ifelse_depth = 0;
                }
                else if (i.type == Items.IF_BRANCH)
                {
                    Node condition = ParseBoolean(iterator.get(), ref iterator, new Tokenized(BasicTokens.SPECIAL, ":"));
                    items.add(i);
                    items.push();
                    items.add(condition);
                    ifelse_depth++;
                }
                else if (i.type == Items.ELSE_BRANCH)
                {
                    items.pop();
                    items.add(i);
                    items.push();
                }
                else if (i.type == Items.EXIT)
                {
                    if (items.top().type != Items.JUMPTO_LABEL)
                    {
                        throw new System.Exception("Invalid use of the 'exit' keyword");
                    }
                    items.add(i);
                }
                else
                {
                    items.add(i);
                }
                break;

            case BasicTokens.STRING:
                i = TokenizeString(token, ref iterator);
                indent_last_count = indent_count;
                indent_count      = 0;
                if (i.type == Items.CHOICE_LABEL)
                {
                    if (defining_choice)
                    {
                        items.pop();
                        items.add(i);
                        items.push();
                    }
                    else if (items.top().type == Items.DIALOGUE)
                    {
                        defining_choice = true;
                        items.push();
                        items.add(i);
                        items.push();
                    }
                    else
                    {
                        throw new System.Exception("Cannot define choice label \"" + i.data[0] + "\" here");
                    }
                }
                else if (i.type == Items.DIALOGUE)
                {
                    if (defining_choice)
                    {
                        defining_choice = false;
                        items.pop();
                        items.pop();
                    }
                    items.add(i);
                }
                break;
            }
        }

        Node rt = items.first();

        db.AddData(stateLabels);
    }
    static private string ParseString(Tokenized token, ref TokenIterator iter)
    {
        bool      found  = true;
        Tokenized extra  = null;
        Tokenized ending = null;

        try
        {
            extra = iter.get(BasicTokens.SPECIAL);
        }
        catch
        {
            found = false;
        }
        if (found)
        {
            if (extra.value != "%")
            {
                return(token.value);
            }

            string[] elems = token.value.Split(new char[] { '%' });
            if (elems.Length == 0)
            {
                throw new System.Exception("Invalid characters " + extra.value + " after string at line " + token.line.ToString());
            }

            string finish = elems[0];
            int    index  = 1;
            while (ending == null || ending.type != BasicTokens.NEW_LINE)
            {
                if (index >= elems.Length)
                {
                    throw new System.Exception("Incorrect number of arguments after string at line " + token.line.ToString());
                }

                extra = iter.get(new BasicTokens[] { BasicTokens.STRING, BasicTokens.VARIABLE, BasicTokens.INTEGER, BasicTokens.BOOLEAN, BasicTokens.COMMAND });
                string data = (extra.type == BasicTokens.STRING) ? ParseString(extra, ref iter) : extra.value;

                finish += "%" + extra.type.ToString() + "%" + data + "%";
                finish += elems[index];
                index++;

                try
                {
                    ending = iter.get(new BasicTokens[] { BasicTokens.SPECIAL });
                }
                catch
                {
                    break;
                }
                if (ending.type == BasicTokens.SPECIAL && ending.value != ",")
                {
                    break;
                }
            }

            return(finish);
        }
        else
        {
            return(token.value);
        }
    }