Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

fix a mistaken comment
Source Link
Pseudo Nym
  • 511
  • 2
  • 5
# preexec state_alias_array command -> state_array command_arg alias_array
preexec ← (
 ⊡1,∶ # put state on top
 !→→(⊡2) # get aliases (for later)
 →(.!⊡1) # get variable name (put two copies on stack for later)
 ≡⊢. # get list of current variable names 
 ¬/+≡(|2 ≅!) →(↯→∶⧻). # test if we need to add a new variable
 ⊂↯→(⊟∶しろいしかく0しろいしかく,) # add variable if necessary
)
# exec<T> state_alias_array command -> state_alias_array
execp ← (
 preexec
 &pf⊂∶" = ", # print "varName = "
 &p/+∺(×ばつ≅→∶∷!⊢∶⊡1.),∶ # print "varValue\n"
 ⊂しろいしかく1⊟しろいしかく
)
execi ← (
 preexec
 ∺([∶∷しろいしかく]+→(∶→≅.)!⊡1∶!⊢.) # increment the relevant variable
 ⊂しろいしかく1⊟しろいしかく # reform state_alias_array
)
execd ← (
 preexec
 ∺([∶∷しろいしかく]-∶→(∶→≅.)!⊡1∶!⊢.) # decrement the relevant variable
 ×ばつ≡(≥0!⊡1). # check if <0
 →≡(⍜'⊡1⍜!'↥0) # reset back up to 0
 ⊂⊟→しろいしかく # reform state_alias_array
)
execcall ← (|2
 ⊢, # get alias name
 ⊗∶≡⊢!⊡2, # get index of call
 ↘1⊡∶!⊡2, # get subcommands
 →,
 ∺(|2
 ⊜しろいしかく≠,@ ! # split box"thing 1 2" into [box"thing" box"1" box"2"]
 ⍜'↘1∺(|2 しろいしかく⍣(!⊡parse)(→;;)!) # turn the 1 and 2 into first and second arguments
 )
 →→; # original call no longer necessary
 しろいしかく/(⊂⊂∶@ ∷!)⊢,∶ # join [box"i" box"dog"] into box"i dog"
 ↬2∶ # call first
 ⊡-∶2!⊢,∶ # select second or third based on success
 しろいしかく/(⊂⊂∶@ ∷!) # join [box"i" box"dog"] into box"i dog"
 ↬2∶
)
execline ← (|2
 ∶⊜しろいしかく≠@ .!∶ # split command
 !⊡∶execp_execi_execd_execcall⊗∶["p""i""d"]!⊢, # dispatch correct command
)
&ru@00円
⊜しろいしかく≠@\n. # split lines
∵⍜!(
 ▽=0\+=@#. # remove comments
 ×ばつ≠@\r∶≠@ ..⇌ # remove trailing whitespace
)
▽↧1∵⧻. # remove blank lines
▽∵(|1 ↥=@ ⊢∶≅"a ".↙2!).. # get alias definitions
⊜· ▽∵(4;).+1⇡÷4⧻. # separate out each alias
≡(
 ⍜⊢(⍜!(↘2)) # remove alias decl
 ⍜'↘1(∵⍜!(↘1)) # remove indentation
)
→(▽¬∵(|1 ↥=@ ∶=@a.⊢!).) # remove alias definitions from command list
⊂{1 [{"123illegalvariable" 0}]}しろいしかく # group last success, state array and alias array
∧execline # tmp: exec firstall instructioninstructions
;
# preexec state_alias_array command -> state_array command_arg alias_array
preexec ← (
 ⊡1,∶ # put state on top
 !→→(⊡2) # get aliases (for later)
 →(.!⊡1) # get variable name (put two copies on stack for later)
 ≡⊢. # get list of current variable names 
 ¬/+≡(|2 ≅!) →(↯→∶⧻). # test if we need to add a new variable
 ⊂↯→(⊟∶しろいしかく0しろいしかく,) # add variable if necessary
)
# exec<T> state_alias_array command -> state_alias_array
execp ← (
 preexec
 &pf⊂∶" = ", # print "varName = "
 &p/+∺(×ばつ≅→∶∷!⊢∶⊡1.),∶ # print "varValue\n"
 ⊂しろいしかく1⊟しろいしかく
)
execi ← (
 preexec
 ∺([∶∷しろいしかく]+→(∶→≅.)!⊡1∶!⊢.) # increment the relevant variable
 ⊂しろいしかく1⊟しろいしかく # reform state_alias_array
)
execd ← (
 preexec
 ∺([∶∷しろいしかく]-∶→(∶→≅.)!⊡1∶!⊢.) # decrement the relevant variable
 ×ばつ≡(≥0!⊡1). # check if <0
 →≡(⍜'⊡1⍜!'↥0) # reset back up to 0
 ⊂⊟→しろいしかく # reform state_alias_array
)
execcall ← (|2
 ⊢, # get alias name
 ⊗∶≡⊢!⊡2, # get index of call
 ↘1⊡∶!⊡2, # get subcommands
 →,
 ∺(|2
 ⊜しろいしかく≠,@ ! # split box"thing 1 2" into [box"thing" box"1" box"2"]
 ⍜'↘1∺(|2 しろいしかく⍣(!⊡parse)(→;;)!) # turn the 1 and 2 into first and second arguments
 )
 →→; # original call no longer necessary
 しろいしかく/(⊂⊂∶@ ∷!)⊢,∶ # join [box"i" box"dog"] into box"i dog"
 ↬2∶ # call first
 ⊡-∶2!⊢,∶ # select second or third based on success
 しろいしかく/(⊂⊂∶@ ∷!) # join [box"i" box"dog"] into box"i dog"
 ↬2∶
)
execline ← (|2
 ∶⊜しろいしかく≠@ .!∶ # split command
 !⊡∶execp_execi_execd_execcall⊗∶["p""i""d"]!⊢, # dispatch correct command
)
&ru@00円
⊜しろいしかく≠@\n. # split lines
∵⍜!(
 ▽=0\+=@#. # remove comments
 ×ばつ≠@\r∶≠@ ..⇌ # remove trailing whitespace
)
▽↧1∵⧻. # remove blank lines
▽∵(|1 ↥=@ ⊢∶≅"a ".↙2!).. # get alias definitions
⊜· ▽∵(4;).+1⇡÷4⧻. # separate out each alias
≡(
 ⍜⊢(⍜!(↘2)) # remove alias decl
 ⍜'↘1(∵⍜!(↘1)) # remove indentation
)
→(▽¬∵(|1 ↥=@ ∶=@a.⊢!).) # remove alias definitions from command list
⊂{1 [{"123illegalvariable" 0}]}しろいしかく # group last success, state array and alias array
∧execline # tmp: exec first instruction
;
# preexec state_alias_array command -> state_array command_arg alias_array
preexec ← (
 ⊡1,∶ # put state on top
 !→→(⊡2) # get aliases (for later)
 →(.!⊡1) # get variable name (put two copies on stack for later)
 ≡⊢. # get list of current variable names 
 ¬/+≡(|2 ≅!) →(↯→∶⧻). # test if we need to add a new variable
 ⊂↯→(⊟∶しろいしかく0しろいしかく,) # add variable if necessary
)
# exec<T> state_alias_array command -> state_alias_array
execp ← (
 preexec
 &pf⊂∶" = ", # print "varName = "
 &p/+∺(×ばつ≅→∶∷!⊢∶⊡1.),∶ # print "varValue\n"
 ⊂しろいしかく1⊟しろいしかく
)
execi ← (
 preexec
 ∺([∶∷しろいしかく]+→(∶→≅.)!⊡1∶!⊢.) # increment the relevant variable
 ⊂しろいしかく1⊟しろいしかく # reform state_alias_array
)
execd ← (
 preexec
 ∺([∶∷しろいしかく]-∶→(∶→≅.)!⊡1∶!⊢.) # decrement the relevant variable
 ×ばつ≡(≥0!⊡1). # check if <0
 →≡(⍜'⊡1⍜!'↥0) # reset back up to 0
 ⊂⊟→しろいしかく # reform state_alias_array
)
execcall ← (|2
 ⊢, # get alias name
 ⊗∶≡⊢!⊡2, # get index of call
 ↘1⊡∶!⊡2, # get subcommands
 →,
 ∺(|2
 ⊜しろいしかく≠,@ ! # split box"thing 1 2" into [box"thing" box"1" box"2"]
 ⍜'↘1∺(|2 しろいしかく⍣(!⊡parse)(→;;)!) # turn the 1 and 2 into first and second arguments
 )
 →→; # original call no longer necessary
 しろいしかく/(⊂⊂∶@ ∷!)⊢,∶ # join [box"i" box"dog"] into box"i dog"
 ↬2∶ # call first
 ⊡-∶2!⊢,∶ # select second or third based on success
 しろいしかく/(⊂⊂∶@ ∷!) # join [box"i" box"dog"] into box"i dog"
 ↬2∶
)
execline ← (|2
 ∶⊜しろいしかく≠@ .!∶ # split command
 !⊡∶execp_execi_execd_execcall⊗∶["p""i""d"]!⊢, # dispatch correct command
)
&ru@00円
⊜しろいしかく≠@\n. # split lines
∵⍜!(
 ▽=0\+=@#. # remove comments
 ×ばつ≠@\r∶≠@ ..⇌ # remove trailing whitespace
)
▽↧1∵⧻. # remove blank lines
▽∵(|1 ↥=@ ⊢∶≅"a ".↙2!).. # get alias definitions
⊜· ▽∵(4;).+1⇡÷4⧻. # separate out each alias
≡(
 ⍜⊢(⍜!(↘2)) # remove alias decl
 ⍜'↘1(∵⍜!(↘1)) # remove indentation
)
→(▽¬∵(|1 ↥=@ ∶=@a.⊢!).) # remove alias definitions from command list
⊂{1 [{"123illegalvariable" 0}]}しろいしかく # group last success, state array and alias array
∧execline # exec all instructions
;
Source Link
Pseudo Nym
  • 511
  • 2
  • 5

Uiua (tentative), 468 characters

Note that due to a bug in the interpreter, there is a spurious error preventing proper execution. Once the bug is fixed, I will update this answer to reflect that. Until then, I'm leaving the (tentative) in the heading.

p←⊂↯→(⊟∶しろいしかく0しろいしかく,)¬/+≡(|2≅!)→(↯→∶⧻).≡⊢.→(.!⊡1)!→→(⊡2)⊡1,∶
;∧(|2!⊡∶(⊂しろいしかく1⊟しろいしかく&p/+∺(×ばつ≅→∶∷!⊢∶⊡1.),∶&pf⊂∶" = ",p)_(⊂しろいしかく1⊟しろいしかく∺([∶∷しろいしかく]+→(∶→≅.)!⊡1∶!⊢.)p)_(⊂⊟→しろいしかく→≡(⍜'⊡1⍜!'↥0)×ばつ≡(≥0!⊡1).∺([∶∷しろいしかく]-∶→(∶→≅.)!⊡1∶!⊢.)p)_(|2↬2∶しろいしかく/(⊂⊂∶@ ∷!)⊡-∶2!⊢,∶↬2∶しろいしかく/(⊂⊂∶@ ∷!)⊢,∶→→;∺(|2⍜'↘1∺(|2しろいしかく⍣(!⊡parse)(→;;)!)⊜しろいしかく≠,@ !)→,↘1⊡∶!⊡2,⊗∶≡⊢!⊡2,⊢,)⊗∶["p""i""d"]!⊢,∶⊜しろいしかく≠@ .!∶)⊂{1[{"1"0}]}しろいしかく→(▽¬∵(|1↥=@ ∶=@a.⊢!).)≡(⍜'↘1(∵⍜!(↘1))⍜⊢(⍜!(↘2)))⊜·▽∵(4;).+1⇡÷4⧻.▽∵(|1↥=@ ⊢∶≅"a ".↙2!)..▽↧1∵⧻.∵⍜!(×ばつ≠@\r∶≠@ ..⇌▽=0\+=@#.)⊜しろいしかく≠@\n.&ru@00円

This is my first Uiua program of more than a couple dozen characters, so I'm sure there's lots of places I could golf it more effectively. It was a good opportunity to practice the language, though :)

Non-minified version

# preexec state_alias_array command -> state_array command_arg alias_array
preexec ← (
 ⊡1,∶ # put state on top
 !→→(⊡2) # get aliases (for later)
 →(.!⊡1) # get variable name (put two copies on stack for later)
 ≡⊢. # get list of current variable names 
 ¬/+≡(|2 ≅!) →(↯→∶⧻). # test if we need to add a new variable
 ⊂↯→(⊟∶しろいしかく0しろいしかく,) # add variable if necessary
)
# exec<T> state_alias_array command -> state_alias_array
execp ← (
 preexec
 &pf⊂∶" = ", # print "varName = "
 &p/+∺(×ばつ≅→∶∷!⊢∶⊡1.),∶ # print "varValue\n"
 ⊂しろいしかく1⊟しろいしかく
)
execi ← (
 preexec
 ∺([∶∷しろいしかく]+→(∶→≅.)!⊡1∶!⊢.) # increment the relevant variable
 ⊂しろいしかく1⊟しろいしかく # reform state_alias_array
)
execd ← (
 preexec
 ∺([∶∷しろいしかく]-∶→(∶→≅.)!⊡1∶!⊢.) # decrement the relevant variable
 ×ばつ≡(≥0!⊡1). # check if <0
 →≡(⍜'⊡1⍜!'↥0) # reset back up to 0
 ⊂⊟→しろいしかく # reform state_alias_array
)
execcall ← (|2
 ⊢, # get alias name
 ⊗∶≡⊢!⊡2, # get index of call
 ↘1⊡∶!⊡2, # get subcommands
 →,
 ∺(|2
 ⊜しろいしかく≠,@ ! # split box"thing 1 2" into [box"thing" box"1" box"2"]
 ⍜'↘1∺(|2 しろいしかく⍣(!⊡parse)(→;;)!) # turn the 1 and 2 into first and second arguments
 )
 →→; # original call no longer necessary
 しろいしかく/(⊂⊂∶@ ∷!)⊢,∶ # join [box"i" box"dog"] into box"i dog"
 ↬2∶ # call first
 ⊡-∶2!⊢,∶ # select second or third based on success
 しろいしかく/(⊂⊂∶@ ∷!) # join [box"i" box"dog"] into box"i dog"
 ↬2∶
)
execline ← (|2
 ∶⊜しろいしかく≠@ .!∶ # split command
 !⊡∶execp_execi_execd_execcall⊗∶["p""i""d"]!⊢, # dispatch correct command
)
&ru@00円
⊜しろいしかく≠@\n. # split lines
∵⍜!(
 ▽=0\+=@#. # remove comments
 ×ばつ≠@\r∶≠@ ..⇌ # remove trailing whitespace
)
▽↧1∵⧻. # remove blank lines
▽∵(|1 ↥=@ ⊢∶≅"a ".↙2!).. # get alias definitions
⊜· ▽∵(4;).+1⇡÷4⧻. # separate out each alias
≡(
 ⍜⊢(⍜!(↘2)) # remove alias decl
 ⍜'↘1(∵⍜!(↘1)) # remove indentation
)
→(▽¬∵(|1 ↥=@ ∶=@a.⊢!).) # remove alias definitions from command list
⊂{1 [{"123illegalvariable" 0}]}しろいしかく # group last success, state array and alias array
∧execline # tmp: exec first instruction
;

AltStyle によって変換されたページ (->オリジナル) /