k-legacy
k-legacy copied to clipboard
problem with CLASS condition of cobol in K
hi, i'm trying to implement the class condition of cobol in k, i have already done the if condition, relation condition, negated condition, combined condition and sign condition. For the class condition i must check if the string is NUMERIC, ALPHABETIC, ALPHABETIC-LOWER, or ALPHABETIC-UPPER. I have already done this rule I1:String IS ALPHABETIC-LOWER => (ordChar(I1) >=Int ordChar("a")) AND (ordChar(I1) <=Int ordChar("z")) but this work only if the I1 string is a single character, and i can't implement a working while loop can someone help me with this problem? sorry for my bad english. @cos @grosu @dwightguth imp.pdf
You can get a character in each position using substrString
. For example, substrString(s, i, i+1)
will return the i-th character of s
.
https://github.com/kframework/k/blob/master/k-distribution/include/builtin/domains.k#L422
Using the above, you can traverse the string by having a recursive rule. For example,
syntax KItem ::= foo(String, Int, Int)
rule foo(S, I, N) => substrString(S, I, I +Int 1) ~> foo(S, I +Int 1, N) when I <Int N
rule foo(S, I, N) => . when I >=Int N
while the initial term could be foo(S, 0, lengthString(S))
.
hi, thanks for the answer, i have solved the problem. but now i have another problem, i must implement the CONDITION NAME condition of COBOL. i'm thinking to store the last declared variable to use this later, but i have no idea how to do this