Ejemplo n.º 1
0
        /// <summary>
        /// This method visits an expressionterm node
        /// First it checks the type on the lefthand side of the expression is either Apin or Dpin
        /// If also checks if an input is attempted to be uses as an output
        /// The input for Apin or Dpin is then accepted and written to the file
        /// Lastly the lefthand side of the expression is accepted
        /// </summary>
        /// <param name="expressionTermNode">The name of the node</param>
        /// <returns>It returns the left side of an expression</returns>
        public override object Visit(ExpressionTerm expressionTermNode)
        {
            string exp = "";

            if (expressionTermNode.LeftHand.IsType(typeof(APinNode)) || expressionTermNode.LeftHand.IsType(typeof(DPinNode)))
            {
                string pin = ((PinNode)expressionTermNode.LeftHand).Value;
                if (PinDefs.Any(def => def.Contains(pin) && def.Contains("OUTPUT")))
                {
                    new InvalidCodeException($"Pin {pin} was defined as OUTPUT but is also used as INPUT at {expressionTermNode.Line}:{expressionTermNode.Offset}");
                }
                if (expressionTermNode.LeftHand.Type == TokenType.APIN)
                {
                    PinDefs.Add($"pinMode({pin}, INPUT);");
                    exp += $"analogRead({pin})";
                }
                else
                {
                    PinDefs.Add($"pinMode({pin}, INPUT);");
                    if (PWM.Contains(pin))
                    {
                        exp += $"analogRead({pin})";
                    }
                    else
                    {
                        exp += $"digitalRead({pin})";
                    }
                }
                return(exp);
            }
            exp += expressionTermNode.LeftHand.Accept(this);
            return(exp);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method first checks if the left side of the assignment is a Dpin, and Apin has the same input as output
        /// Create the Apin or Dpin as the lefthand side is accepted
        /// If it's a PWM pin, its an Apin otherwise its a Dpin
        /// It then accepts the righthand side and if its numeric, the value is accepted
        /// It can also be assigned to true, high or low
        /// The method can also accept the righthand side is an array
        /// </summary>
        /// <param name="assignmentNode">The name of the node</param>
        /// <returns>It returns an assignment</returns>
        public override object Visit(AssignmentNode assignmentNode)
        {
            string assign = "";

            if (assignmentNode.LeftHand.Type == TokenType.DPIN)
            {
                string pin = (string)assignmentNode.LeftHand.Accept(this);
                if (PinDefs.Any(def => def.Contains(pin) && def.Contains("INPUT")))
                {
                    new InvalidCodeException($"Pin {pin} was defined as INPUT but is also used as OUTPUT at {assignmentNode.Line}:{assignmentNode.Offset}");
                }
                string pinDef = "pinMode(" + assignmentNode.LeftHand.Accept(this) + ", OUTPUT);";
                PinDefs.Add(pinDef);
                if (PWM.Contains(pin))
                {
                    assign += "analogWrite(" + assignmentNode.LeftHand.Accept(this) + ", ";
                    string value = assignmentNode.RightHand.Accept(this) + ")";
                    if (assignmentNode.RightHand.SymbolType.Type == TokenType.NUMERIC)
                    {
                        assign += value;
                    }
                    else if (value.StartsWith("digitalRead"))
                    {
                        assign += value;
                    }
                    else
                    {
                        assign += value == " true)" ? "HIGH)" : "LOW)";
                    }
                }
                else
                {
                    assign += "digitalWrite(" + assignmentNode.LeftHand.Accept(this) + ", ";
                    string boolValue = assignmentNode.RightHand.Accept(this) + ")";
                    if (boolValue.StartsWith("digitalRead"))
                    {
                        assign += boolValue;
                    }
                    else
                    {
                        assign += boolValue == " true)" ? "HIGH)" : "LOW)";
                    }
                }
            }
            else if (assignmentNode.LeftHand.Type == TokenType.APIN)
            {
                string pin = (string)assignmentNode.LeftHand.Accept(this);
                if (PinDefs.Any(def => def.Contains(pin) && def.Contains("INPUT")))
                {
                    new InvalidCodeException($"Pin {pin} was defined as INPUT but is also used as OUTPUT at {assignmentNode.Line}:{assignmentNode.Offset}");
                }
                string pinDef = "pinMode(" + assignmentNode.LeftHand.Accept(this) + ", OUTPUT);";
                PinDefs.Add(pinDef);

                assign += "analogWrite(" + assignmentNode.LeftHand.Accept(this) + ", ";
                string boolValue = assignmentNode.RightHand.Accept(this) + ")";
                if (assignmentNode.RightHand.SymbolType.Type == TokenType.NUMERIC)
                {
                    assign += boolValue;
                }
                else if (boolValue.StartsWith("analogRead"))
                {
                    assign += boolValue;
                }
                else
                {
                    assign += boolValue == " true)" ? "HIGH)" : "LOW)";
                }
            }
            else
            {
                assign += (string)assignmentNode.LeftHand.Accept(this);
                if (assignmentNode.RightHand.IsType(typeof(ArrayNode)))
                {
                    assign += (string)assignmentNode.RightHand.Accept(this);
                    assign += ";";
                    return(assign);
                }
                assign += " = ";
                assign += (string)assignmentNode.RightHand.Accept(this);
            }

            assign += ";";
            return(assign);
        }