create ++ operator in VHDL - function

I would like to have a new C++ style operator for the STD_LOGIC_VECTOR type. So far I managed to create and use the following function:
FUNCTION PLUS_ONE ( a : STD_LOGIC_VECTOR) RETURN STD_LOGIC_VECTOR is
BEGIN
RETURN std_logic_vector( unsigned( a ) + 1);
END FUNCTION;
now if i create this:
FUNCTION "++" ( a : STD_LOGIC_VECTOR) RETURN STD_LOGIC_VECTOR is
BEGIN
RETURN std_logic_vector( unsigned( a ) + 1);
END FUNCTION;
ISE throws the following error:
"++" is not a predefined operator.
Now the question is, is it possible to create new operators in VHDL an I am missing something

You can only overload operators in VHDL, you cannot create new operator symbols. Quoting the LRM (section 4.5.2):
The declaration of a function whose designator is an operator symbol
is used to overload an operator. The sequence of characters of the
operator symbol shall be one of the operators in the operator classes
defined in 9.2.
And the corresponding section of the manual says:
condition_operator ::= ??
logical_operator ::= and | or | nand | nor | xor | xnor
relational_operator ::= = | /= | < | <= | > | >= | ?= | ?/= | ?< | ?<= | ?> | ?>=
shift_operator ::= sll | srl | sla | sra | rol | ror
adding_operator ::= + | – | &
sign ::= + | –
multiplying_operator ::= * | / | mod | rem
miscellaneous_operator ::= ** | abs | not
As much as I like brevity, I must admit that choosing shorthand operators over standard ways of writing expressions is "syntactic sugar", and has a potential to obfuscate the code. It is interesting to note that "trendier" languages like Python and Ruby don't have a ++ operator as well.
Could VHDL support the ++ operator? I'm currently working on a VHDL parser, and I risk saying that adding a postfix ++ operator would break quite a few rules of the language grammar, especially because unary operators expect to take an operand to the right of the symbol. Owing to this and to the fact that aren't many strong arguments in favor of such a change, I don't expect to see it anytime soon. All thigs considered, my personal choice has been to stick with value := value + 1 for standard data types.

Related

F# error FS0588: The block following this 'let' is unfinished. Every code block is an expression and must have a result

I am tasked with finishing an interpreter in F#, but I'm having some trouble, as I im getting the error: error FS0588: The block following this 'let' is unfinished. Every code block is an expression and must have a result. 'let' cannot be the final code element in a block. Consider giving this block an explicit result.
Its been a long time since last time I programmed I F#.
The following is my code. I have a helper function inside my eval function, called OperateAux. It gets called in the pattern matching, when it matches e with OPERATE. It should then call OperateAux, and calculate the given expression. The error I'm getting is at line: let OperateAux (op:BINOP) (e1:EXP) (e2:EXP) : VALUE =
so I guess somehow my helper function isn't finished, I just cant figure out where.
let rec eval (vtab : SymTab) (e : EXP) : VALUE =
match e with
| CONSTANT n -> n
| VARIABLE v -> lookup v vtab
| OPERATE (op, e1, e2) -> OperateAux op e1 e2//(eval vtab e1) (eval vtab e2)
| LET_IN (var, e1, e2) -> failwith "case for LET_IN not handled"
| OVER (rop, var, e1, e2, e3) -> failwith "case for OVER not handled"
let OperateAux (op:BINOP) (e1:EXP) (e2:EXP) : VALUE =
let (INT e1) = eval vtab e1
let (INT e2) = eval vtab e2
match op with
| BPLUS -> (e1+e2)
| BMINUS -> (e1-e2)
| BTIMES -> (e1*e2)
| _ -> ()
Here is some types, I'm not sure if they are relevant for this question, but for good measure I'll show them.
type VALUE = INT of int
type BINOP = BPLUS | BMINUS | BTIMES
type RANGEOP = RSUM | RPROD | RMAX | RARGMAX
type EXP =
| CONSTANT of VALUE
| VARIABLE of string
| OPERATE of BINOP * EXP * EXP
| LET_IN of string * EXP * EXP
| OVER of RANGEOP * string * EXP * EXP * EXP
(* A list mapping variable names to their values. *)
type SymTab = (string * VALUE) list
Nevermind, I figured it out. You have to "initialise" your helper function before actually calling it. So the helper function operateAux should come before the pattern matching which calls it.

Display symbolic expression in octave. Matrix multiplication as an expression and not as a result

I have hard time finding out how to display matrix multiplication as an expression, not as a result of an expression. The expression must be displayed in command line, not as a plot.
Lets say I have
syms m00 m01 m10 m11;
M = [m00 m01; m10 m11];
syms x0 x1;
X = [x0; x1];
I want to see the expression M * X as a symbolic expression. Something that will be displayed like:
| m00 m01 | * | x0 |
| m10 m11 | | x1 |
And will not be displayed as a result of M * X evaluation:
| m00*x0 + m01*x1 |
| m10*x0 + m11*x1 |
I have read documentation on octave symbolic package. Can not seem to find the mechanics there. My thoughts are wrapped around converting expressions to latex,
m = latex(M)
x = latex(X)
Concatenating the result as a latex string, and somehow print the latex string in octave command line. No luck as of now.

Using ANTLR4 to create functions with no argument

I am still new to ANTLR4 and I am trying to achieve the following
I have business rules which consist of logical operation
(A= 'text' or B < 1) and getDataDB
the function getDataDB does not take any argument. the function will retrieve some data to validate it and return either true or false.
my grammar is below
/*
* Test grammar
*/
grammar FunctionRule;
parse: expr EOF
;
expr
: expr binop expr #logicalExpression
| lhs=VARIABLE compop rhs=VARIABLE #variableExpression
| lhs=VARIABLE compop rhs=STRING #stringExpression
| lhs=VARIABLE compop rhs=NUMBER #numberExpression
| TRUE #booleanTrue
| FALSE #booleanFalse
| function #functionExpression
| VARIABLE #booleanVariable
| LEFTPAREN expr RIGHTPAREN #enclosedExpression
;
binop : AND | OR
;
compop: EQUAL | LT | GT | LTE | GTE | NE
;
function : ID {System.out.println("HELLLL");};
TRUE: 'true' | 'TRUE' ;
FALSE: 'false' | 'FALSE';
STRING: '"' ~([\t\n\r]| '"')* '"'
;
ID : [getDataDB];
LEFTPAREN: '(';
RIGHTPAREN: ')';
EQUAL : '=' | 'EQ';
LT : '<' | 'LT';
GT : '>' | 'GT';
LTE : '<=' | 'LE';
GTE : '>=' | 'GE';
NE : '!=' | 'NE';
AND : 'AND' | '&' | 'and';
OR : 'OR' | 'or' | '|';
VARIABLE : [a-zA-Z]+[a-zA-Z0-9_.-]*;
NUMBER : [0-9]+ ('.'[0-9]+)?;
SPACE : [ \t\r\n] -> skip;
When I generate classes from the grammar, i did not see anything related to the function.
1-how do I define a function correctly in the grammar file.
2- where i can put the code for this function after creating the classes, is it only in the action clause, is there is a way to put the class name in the grammar where i can put the implementation
Thanks for the help!
ID : [getDataDB];
This means that ID matches a single letter that could be either one of g, e, t, D, a or B. What you likely wanted is ID: 'getDataDB'; which matches the string getDataDB. Note that calling this ID is highly misleading.
where i can put the code for this function
Are you writing an interpreter using a visitor? Then you'd put the code into the visitFunction method or rather in a getDataDB method that you call from visitFunction if the function name was equal to getDataDB (right now that would always be the case, but I'm assuming you eventually want to introduce more than one function).
Alternatively you could also structure your grammar slightly differently like this (removing the ID rule):
function : 'getDataDB' # GetDataDB
| 'otherFunction' # OtherFunction
;
Then you could define the functions in visitGetDataDB and visitOtherFunction respectively.
All that's assuming that you want function names to be keywords (which implies that there can't be user-definable functions). If you don't, you should not have separate tokens for function names, so zero-argument functions and variables become indistinguishable syntactically (unless you add a requirement to add () for functions, but it doesn't look like that's what you want). So you should just have one rule that could be either a variable or a zero-argument function and then check whether the given identifier is the name of a function in visitVariableOrNullaryFunction (which maybe you'd just call visitVariable for brevity).

Haskell Integer Odd Digits Checker

I seem to be stuck on a question and have no idea how to approach it or what Im doing wrong with my current code.
I have to write a function called oddDigits which takes a single integer argument and returns a boolean result. It should return True if and only if the argument is a positive integer with an odd number of digits. If the argument is zero or negative, the function should stop with an error message.
Also, cant convert the argument into a string. Have to use recursion.
I have a feeling each digit could be stored in a list recursively and then the length of the list could determine the answer.
So far, I have this:
oddDigits :: Integer -> Bool
lst = []
oddDigits x
| (x < 0) || (x == 0) = error
| x `mod` 10 ++ lst ++ oddDigits(x `div` 10)
| length(lst) `mod` 2 /= 0 = True
| otherwise = False
Sorry if the code looks horrible. I am new to Haskell and still learning. What exactly am I doing wrong and how could I correct it?
First off, this seems a pretty weird thing to check. Perhaps what you're doing wrong is to ever consider this problem...
But if you persist you want to know the property of an integer having an odd number of digits... oh well. There's a lot that could be improved. For starters, (x < 0) || (x == 0) doesn't need the parentheses – < and == (infix 4) bind more tightly than ||. If you're not sure about this, you can always ask GHCi:
Prelude> :i ==
class Eq a where
(==) :: a -> a -> Bool
...
-- Defined in ‘GHC.Classes’
infix 4 ==
Prelude> :i ||
(||) :: Bool -> Bool -> Bool -- Defined in ‘GHC.Classes’
infixr 2 ||
But here you don't need || anyway because there's a dedicated operator for less-than-or-equal. Hence you can just write
oddDigits x
| x <= 0 = error "bla bla"
| ...
Then, you can “convert” the number to a string. Converting to string is generally a really frowned-upon thing to do because it throws all structure, typechecking etc. out of the window; however “number of digits” basically is a property of a string (the decimal expansion), rather than a number itself, so this is not entirely unsensible for this specific task. This would work:
oddDigits x
| x <= 0 = error "blearg"
| length (show x)`mod`2 /= 0 = True
| otherwise = False
however it's a bit redundancy department redundant. You're checking if something is True, then give True as the result... why not just put it in one clause:
oddDigits x
| x <= 0 = error "blearg"
| otherwise = length (show x)`mod`2 /= 0
That's perhaps in fact the best implementation.
For any proper, sensible task, I would not recommend going the string route. Recursion is better. Here's what it could look like:
oddDigits 1 = True
oddDigits x
| x <= 0 = error "blearg"
| otherwise = not . oddDigits $ x`div`10
There's nothing wrong with your general approach of converting to a list of digits, then finding the length of the list. Really where you went wrong is trying to cram everything into one function. As you found out first hand, it makes it very difficult to debug. Functional programming works best with very small functions.
If you separate out the responsibility of converting an integer to a list of digits, using a digs function like the one from this answer, the rest of your algorithm simplifies to:
oddDigits x | x <= 0 = error
oddDigits x = odd . length $ digs x
leftaroundabout's eventual answer is very nice, however it fails for numbers like 2,3 and 23. Here's a fix.
oddDigits x
| x <= 0 = error "blearg"
| x < 10 = True
| otherwise = not . oddDigits $ x`div`10
Its much more elegant than my initial answer, below. I'm including it to introduce a common functional paradigm, a worker/wrapper transformation of the problem. Here the wrapper gives the interface and passes off the work to another function. Notice that the negativity check only needs to be done once now.
oddDigits :: Integer -> Bool
oddDigits x
| x <= 0 = False
| otherwise = oddDigits' True x
oddDigits' :: Bool -> Integer -> Bool
oddDigits' t x
| x < 10 = t
| otherwise = oddDigits' (not t) $ x `div` 10
oddDigits' carries a piece of internal data with it, the initial Bool. My first first thought was to have that Bool be a digit accumulator, counting the number of digits. In that case, an "unwrapper" needs to be supplied, in this case the standard "odd" function:
oddDigits x
| x <= 0 = False
| otherwise = odd . oddDigits'' 1 $ x
where oddDigits'' :: Integer -> Integer -> Integer.

ANTLR: problem differntiating unary and binary operators (e.g. minus sign)

i'm using ANTLR (3.2) to parse some rather simple grammar. Unfortunately, I came across a little problem. Take the follwoing rule:
exp
: NUM
| '(' expression OPERATOR expression ')' -> expression+
| '(' (MINUS | '!') expression ')' -> expression
;
OPERATOR contains the same minus sign ('-') as is defined with MINUS. Now ANTLR seems to be unable to deal with these two rules. If I remove either one, everything works fine.
Anyone ideas?
Make the unary expression the one with the highest precedence. I'd also use a different token for the unary - to make the distinction between the minus better. A demo:
grammar Exp;
options {
output=AST;
}
tokens {
UNARY;
}
parse
: exp EOF
;
exp
: additionExp
;
additionExp
: multiplyExp ('+'^ multiplyExp | '-'^ multiplyExp)*
;
multiplyExp
: unaryExp ('*'^ unaryExp | '/'^ unaryExp)*
;
unaryExp
: '-' atom -> ^(UNARY atom)
| '!' atom -> ^('!' atom)
| atom
;
atom
: '(' exp ')' -> exp
| Number -> Number
;
Number : ('0'..'9')+ ('.' ('0'..'9')+)? ;
Spaces : (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;} ;
A quick test with the source:
3 * -4 + 7 / 6 * -(3 + -7 * (4 + !2))
produced the following AST:
image created using http://graph.gafol.net/