September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,203 @@
/* ARM assembly Raspberry PI */
/* program incstring.s */
/* Constantes */
.equ BUFFERSIZE, 100
.equ STDIN, 0 @ Linux input console
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ READ, 3 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
/* Initialized data */
.data
szMessNum: .asciz "Enter number : \n"
szCarriageReturn: .asciz "\n"
szMessResult: .ascii "Increment number is = " @ message result
sMessValeur: .fill 12, 1, ' '
.asciz "\n"
/* UnInitialized data */
.bss
sBuffer: .skip BUFFERSIZE
/* code section */
.text
.global main
main: /* entry of program */
push {fp,lr} /* saves 2 registers */
ldr r0,iAdrszMessNum
bl affichageMess
mov r0,#STDIN @ Linux input console
ldr r1,iAdrsBuffer @ buffer address
mov r2,#BUFFERSIZE @ buffer size
mov r7, #READ @ request to read datas
swi 0 @ call system
ldr r1,iAdrsBuffer @ buffer address
mov r2,#0 @ end of string
strb r2,[r1,r0] @ store byte at the end of input string (r0
@
ldr r0,iAdrsBuffer @ buffer address
bl conversionAtoD @ conversion string in number in r0
@ increment r0
add r0,#1
@ conversion register to string
ldr r1,iAdrsMessValeur
bl conversion10S @ call conversion
ldr r0,iAdrszMessResult
bl affichageMess @ display message
100: /* standard end of the program */
mov r0, #0 @ return code
pop {fp,lr} @restaur 2 registers
mov r7, #EXIT @ request to exit program
swi 0 @ perform the system call
iAdrsMessValeur: .int sMessValeur
iAdrszMessNum: .int szMessNum
iAdrsBuffer: .int sBuffer
iAdrszMessResult: .int szMessResult
iAdrszCarriageReturn: .int szCarriageReturn
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {fp,lr} /* save registres */
push {r0,r1,r2,r7} /* save others registers */
mov r2,#0 /* counter length */
1: /* loop length calculation */
ldrb r1,[r0,r2] /* read octet start position + index */
cmp r1,#0 /* if 0 its over */
addne r2,r2,#1 /* else add 1 in the length */
bne 1b /* and loop */
/* so here r2 contains the length of the message */
mov r1,r0 /* address message in r1 */
mov r0,#STDOUT /* code to write to the standard output Linux */
mov r7, #WRITE /* code call system "write" */
swi #0 /* call systeme */
pop {r0,r1,r2,r7} /* restaur others registers */
pop {fp,lr} /* restaur des 2 registres */
bx lr /* return */
/******************************************************************/
/* Convert a string to a number stored in a registry */
/******************************************************************/
/* r0 contains the address of the area terminated by 0 or 0A */
/* r0 returns a number */
conversionAtoD:
push {fp,lr} @ save 2 registers
push {r1-r7} @ save others registers
mov r1,#0
mov r2,#10 @ factor
mov r3,#0 @ counter
mov r4,r0 @ save address string -> r4
mov r6,#0 @ positive sign by default
mov r0,#0 @ initialization to 0
1: /* early space elimination loop */
ldrb r5,[r4,r3] @ loading in r5 of the byte located at the beginning + the position
cmp r5,#0 @ end of string -> end routine
beq 100f
cmp r5,#0x0A @ end of string -> end routine
beq 100f
cmp r5,#' ' @ space ?
addeq r3,r3,#1 @ yes we loop by moving one byte
beq 1b
cmp r5,#'-' @ first character is -
moveq r6,#1 @ 1 -> r6
beq 3f @ then move on to the next position
2: /* beginning of digit processing loop */
cmp r5,#'0' @ character is not a number
blt 3f
cmp r5,#'9' @ character is not a number
bgt 3f
/* character is a number */
sub r5,#48
umull r0,r1,r2,r0 @ multiply par factor 10
cmp r1,#0 @ overflow ?
bgt 99f @ overflow error
add r0,r5 @ add to r0
3:
add r3,r3,#1 @ advance to the next position
ldrb r5,[r4,r3] @ load byte
cmp r5,#0 @ end of string -> end routine
beq 4f
cmp r5,#0x0A @ end of string -> end routine
beq 4f
b 2b @ loop
4:
cmp r6,#1 @ test r6 for sign
moveq r1,#-1
muleq r0,r1,r0 @ if negatif, multiply par -1
b 100f
99: /* overflow error */
ldr r0,=szMessErrDep
bl affichageMess
mov r0,#0 @ return zero if error
100:
pop {r1-r7} @ restaur other registers
pop {fp,lr} @ restaur 2 registers
bx lr @return procedure
/* constante program */
szMessErrDep: .asciz "Too large: overflow 32 bits.\n"
.align 4
/***************************************************/
/* Converting a register to a signed decimal */
/***************************************************/
/* r0 contains value and r1 area address */
conversion10S:
push {r0-r4,lr} @ save registers
mov r2,r1 /* debut zone stockage */
mov r3,#'+' /* par defaut le signe est + */
cmp r0,#0 @ negative number ?
movlt r3,#'-' @ yes
mvnlt r0,r0 @ number inversion
addlt r0,#1
mov r4,#10 @ length area
1: @ start loop
bl divisionpar10
add r1,#48 @ digit
strb r1,[r2,r4] @ store digit on area
sub r4,r4,#1 @ previous position
cmp r0,#0 @ stop if quotient = 0
bne 1b
strb r3,[r2,r4] @ store signe
subs r4,r4,#1 @ previous position
blt 100f @ if r4 < 0 -> end
mov r1,#' ' @ space
2:
strb r1,[r2,r4] @store byte space
subs r4,r4,#1 @ previous position
bge 2b @ loop if r4 > 0
100:
pop {r0-r4,lr} @ restaur registers
bx lr
/***************************************************/
/* division par 10 signé */
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*
/* and http://www.hackersdelight.org/ */
/***************************************************/
/* r0 dividende */
/* r0 quotient */
/* r1 remainder */
divisionpar10:
/* r0 contains the argument to be divided by 10 */
push {r2-r4} /* save registers */
mov r4,r0
ldr r3, .Ls_magic_number_10 /* r1 <- magic_number */
smull r1, r2, r3, r0 /* r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0) */
mov r2, r2, ASR #2 /* r2 <- r2 >> 2 */
mov r1, r0, LSR #31 /* r1 <- r0 >> 31 */
add r0, r2, r1 /* r0 <- r2 + r1 */
add r2,r0,r0, lsl #2 /* r2 <- r0 * 5 */
sub r1,r4,r2, lsl #1 /* r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) */
pop {r2-r4}
bx lr /* leave function */
.align 4
.Ls_magic_number_10: .word 0x66666667

View file

@ -0,0 +1,107 @@
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
-- succString :: Bool -> String -> String
on succString(blnPruned, s)
script go
on |λ|(w)
try
if w contains "." then
set v to w as real
else
set v to w as integer
end if
{(1 + v) as string}
on error
if blnPruned then
{}
else
{w}
end if
end try
end |λ|
end script
unwords(concatMap(go, |words|(s)))
end succString
-- TEST ---------------------------------------------------
on run
script test
on |λ|(bln)
succString(bln, ¬
"41 pine martens in 1491.3 -1.5 mushrooms ≠ 136")
end |λ|
end script
unlines(map(test, {true, false}))
end run
--> 42 1492.3 -0.5 137
--> 42 pine martens in 1492.3 -0.5 mushrooms ≠ 137
-- GENERIC ------------------------------------------------
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
set lng to length of xs
set acc to {}
tell mReturn(f)
repeat with i from 1 to lng
set acc to acc & |λ|(item i of xs, i, xs)
end repeat
end tell
return acc
end concatMap
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- unlines :: [String] -> String
on unlines(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set str to xs as text
set my text item delimiters to dlm
str
end unlines
-- unwords :: [String] -> String
on unwords(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, space}
set s to xs as text
set my text item delimiters to dlm
return s
end unwords
-- words :: String -> [String]
on |words|(s)
set ca to current application
(((ca's NSString's stringWithString:(s))'s ¬
componentsSeparatedByCharactersInSet:(ca's ¬
NSCharacterSet's whitespaceAndNewlineCharacterSet()))'s ¬
filteredArrayUsingPredicate:(ca's ¬
NSPredicate's predicateWithFormat:"0 < length")) as list
end |words|

View file

@ -1,2 +1,2 @@
10 LET s$ = "12345"
20 LET s$ = STR$(VAL(s$) + 1)
100 LET S$="12345"
110 LET S$=STR$(VAL(S$)+1)

View file

@ -0,0 +1,2 @@
10 LET s$ = "12345"
20 LET s$ = STR$(VAL(s$) + 1)

View file

@ -1,9 +1,9 @@
import extensions.
import extensions;
program =
[
literal s := "12345".
s := s toInt + 1.
public program()
{
var s := "12345";
s := (s.toInt() + 1).toString();
console printLine(s).
].
console.printLine(s)
}

View file

@ -0,0 +1,30 @@
import Text.Read (readMaybe)
import Data.Maybe (mapMaybe)
succString :: Bool -> String -> String
succString pruned s =
let succs
:: (Num a, Show a)
=> a -> Maybe String
succs = Just . show . (1 +)
go w
| elem '.' w = (readMaybe w :: Maybe Double) >>= succs
| otherwise = (readMaybe w :: Maybe Integer) >>= succs
opt w
| pruned = Nothing
| otherwise = Just w
in unwords $
mapMaybe
(\w ->
case go w of
Just s -> Just s
_ -> opt w)
(words s)
-- TEST ---------------------------------------------------
main :: IO ()
main =
(putStrLn . unlines) $
succString <$> [True, False] <*>
pure "41.0 pine martens in 1491 -1.5 mushrooms ≠ 136"

View file

@ -1,14 +1,59 @@
(() => {
'use strict';
// stringSucc :: Maybe String -> Maybe String
const stringSucc = s =>
isNaN(s) ? undefined : (Number(s) + 1).toString();
// succString :: Bool -> String -> String
const succString = blnPruned => s => {
const go = w => {
const
v = w.includes('.') ? (
parseFloat(w)
) : parseInt(w);
return isNaN(v) ? (
blnPruned ? [] : [w]
) : [(1 + v).toString()];
};
return unwords(concatMap(go, words(s)));
};
// show :: a -> String
const show = x => JSON.stringify(x, null, 2);
// TEST -----------------------------------------------
const main = () =>
console.log(
unlines(
ap(
map(succString, [true, false]),
['41 pine martens in 1491.3 -1.5 mushrooms ≠ 136']
)
)
);
return show(
['2', '4', '8', '16', 'anomaly'].map(stringSucc)
);
// GENERIC FUNCTIONS ----------------------------------
// Each member of a list of functions applied to each
// of a list of arguments, deriving a list of new values.
// ap (<*>) :: [(a -> b)] -> [a] -> [b]
const ap = (fs, xs) => //
fs.reduce((a, f) => a.concat(
xs.reduce((a, x) => a.concat([f(x)]), [])
), []);
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = (f, xs) =>
xs.reduce((a, x) => a.concat(f(x)), []);
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
// unwords :: [String] -> String
const unwords = xs => xs.join(' ');
// words :: String -> [String]
const words = s => s.split(/\s+/);
// MAIN ---
return main();
})();

View file

@ -0,0 +1,36 @@
# Dropping or keeping any non-numerics in the string
# succString :: Bool -> String -> String
def succString(blnPruned):
def go(x):
try:
return [str(1 + (float(x) if '.' in x else int(x)))]
except ValueError:
return [] if blnPruned else [x]
return lambda s: ' '.join(concatMap(go)(s.split()))
# TEST ----------------------------------------------------
def main():
print(
'\n'.join(
[succString(bln)(
'41.0 pine martens in 1491 -1.5 mushrooms ≠ 136'
) for bln in [False, True]]
)
)
# GENERIC ---------------------------------------------------
# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
return lambda xs: (
[ys[0] for ys in [f(x) for x in xs] if ys]
)
# MAIN ---
if __name__ == '__main__':
main()

View file

@ -1 +1 @@
"123" toNumber 1+ toString
'123 s:to-number n:inc n:to-string