|
|
In article <slr### [at] fwicom> , Ron Parker
<ron### [at] povrayorg> wrote:
>> Given L is a list,
>> who could tell "every i:=1 to *L/2 do L[i]:=:L[-i]" reverses it?
>
> Hm, that would work in Perl, too, with very few modifications.
>
> Of course, @L = reverse @L would work, too...
I picked a short example. Below is a scanner, also given as *educational*
example (from http://www.iit.edu/~tc/), in particular note sequences like
"||:= =", or the operators for connecting strings, because that is what the
"++" does. Now guess what the "?" does!
Basically all operators are unqiue for each data type, which gives you a farm
of operators. In order to implement those you end up with other interesting
character sequences like "~==="...
Thorsten
procedure scan()
local t,c,b
static whiteSpace,initIdChars,idChars,hexdigits,commentDepth,commentLineNo
initial {
/inputFile := &input
inputLineNumber := 1
inputColumn := 1
inputLine := read(inputFile)
eoiToken := &null
whiteSpace := &ascii[1:34] #control ++ blank
initIdChars := &letters
hexdigits := &digits ++ 'ABCDEF'
idChars := &letters ++ &digits ++ '$_'
keywordSet := set([
"DEF","VAL","while","bu"
])
}
if \eoiToken then return eoiToken
repeat inputLine ? {
tab(inputColumn)
tab(many(whiteSpace))
c := &pos
if b := (tab(any('+-'))|"") ||
tab(many(&digits)) then {
if b := b || fractionPart() ||
scaleFactor() then {
t := Token("signedFloat",b,
inputLineNumber,c)
} else if b ||:= fractionPart() then {
t := Token("signedFloat",b,
inputLineNumber,c)
} else if b ||:= ="." || scaleFactor() then {
t := Token("signedFloat",b,
inputLineNumber,c)
} else {
t := Token("signedInt",b,
inputLineNumber,c)
}
inputColumn := &pos
return t
} else
if any(initIdChars) then {
t := Token("ident",tab(many(idChars)),
inputLineNumber,c)
inputColumn := &pos
if member(keywordSet,t.body) then
t.type := t.body
return t
} else
if b := =("~=" | ">=" | "<=" | "->") then {
inputColumn := &pos
return Token(b,b,inputLineNumber,c)
} else
if ="(*" then {
inputColumn := &pos
commentDepth := 1
commentLineNo := inputLineNumber
while commentDepth > 0 do {
tab(upto('*(')|0)
if pos(0) then {
&pos := 1
inputLineNumber +:= 1
if not (&subject :=
inputLine := read(inputFile))
then {
eoiToken := Token("EOI","EOI",
inputLineNumber,1)
write("end of input in comment beginning at ",
commentLineNo)
return eoiToken
}
} else if ="*)" then {
commentDepth -:= 1
} else if ="(*" then {
commentDepth +:= 1
} else {
move(1)
}
}
inputColumn := &pos
} else
if b := tab(any('\',=()[]<>+-*/@;:.')) then {
inputColumn := &pos
return Token(b,b,inputLineNumber,c)
} else
if pos(0) then {
inputColumn := 1
inputLineNumber +:= 1
if not (inputLine := read(inputFile)) then {
eoiToken := Token("EOI","EOI",
inputLineNumber,1)
return eoiToken
}
} else
if ="\"" then {
b := tab(find("\""))
if not( = "\"" ) then {
write("unterminated string at ",
inputLineNumber," ",c)
}
t := Token("string",b,inputLineNumber,c)
inputColumn := &pos
return t
} else
{
write("unexpected character: ",move(1),
" at line ",inputLineNumber," column ",c)
inputColumn := &pos
}
}
end
Post a reply to this message
|
|