Skip to content

Instantly share code, notes, and snippets.

@novusnota
Created October 27, 2022 11:28
Show Gist options
  • Save novusnota/9a0b19ad5b459ca8ad54549909dfc190 to your computer and use it in GitHub Desktop.
Save novusnota/9a0b19ad5b459ca8ad54549909dfc190 to your computer and use it in GitHub Desktop.
A small Julia-based script, which allows usage of 'and' and 'or' keywords instead of '&&' and '||' infix operators in Julia programs.
#!/usr/bin/env julia
##
# A small script, written in Julia, which allows usage of 'and' and 'or' keywords instead of '&&' and '||' infix operators in Julia programs.
# Made out of desperation after reading this discussion: https://github.com/JuliaLang/julia/issues/5238
# Notice, that run.jl **doesn't** modify the original file.
#
# Use it simply so:
# $> julia run.jl <filename>
# or by giving the file execution rights and then running:
# $> chmod +x run.jl
# $> ./run.jl <filename>
#
# For example, a line in a given program file could look like this:
# if (y == 3 and (x == 4 or x == 5))
# which would be transformed by run.jl on the fly to:
# if (y == 3 && (x == 4 || x == 5))
# and then passed to Julia to evaluate the modified version of the file on the fly.
#
##
# Getting file name from command line argument passed to our program
file_name = ""
try
global file_name = ARGS[1]
catch
println("No arguments passed -- write a name of a file after this program's name")
exit(1)
end
# Reading file contents
file_contents = ""
try
open(file_name, "r") do file
global file_contents = read(file, String)
end
catch
println("File '$file_name' doesn't exist")
exit(1)
end
# Substituting 'and' and 'or' using regular expressions
file_contents = replace(file_contents, r" and " => s" && ")
file_contents = replace(file_contents, r" or " => s" || ")
# Evaluating the final result
eval(Meta.parse(file_contents))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment