Skip to content

Commit

Permalink
Merge pull request #24 from timotheecour/exp_cmd_line
Browse files Browse the repository at this point in the history
fix issue #22 cmd line arg to set compiler and get help; fix issue #16
  • Loading branch information
AndreiRegiani authored Jul 14, 2018
2 parents c32bd4f + cab5701 commit b0bdebd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
1 change: 1 addition & 0 deletions inim.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ bin = @["inim"]
# Dependencies

requires "nim >= 0.17.0"
requires "cligen >= 0.9.15"
68 changes: 43 additions & 25 deletions src/inim.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# MIT License
# Copyright (c) 2018 Andrei Regiani
import os, osproc, rdstdin, strutils, terminal, times
import os, osproc, rdstdin, strutils, terminal, times, strformat

type App = ref object
nim: string
srcFile: string
showHeader: bool

var app:App

const
INimVersion = "0.2.5"
Expand All @@ -11,7 +18,11 @@ const
let
uniquePrefix = epochTime().int
bufferSource = getTempDir() & "inim_" & $uniquePrefix & ".nim"
compileCmd = "nim compile --run --verbosity=0 --hints=off --path=./ " & bufferSource

proc compileCode():auto =
# PENDING https://github.com/nim-lang/Nim/issues/8312, remove redundant `--hint[source]=off`
let compileCmd = fmt"{app.nim} compile --run --verbosity=0 --hints=off --hint[source]=off --path=./ {bufferSource}"
result = execCmdEx(compileCmd)

var
currentOutputLine = 0 # Last line shown from buffer's stdout
Expand All @@ -21,16 +32,16 @@ var
buffer: File

proc getNimVersion*(): string =
let (output, status) = execCmdEx("nim --version")
if status != 0:
echo "inim: Program \"nim\" not found in PATH"
quit(1)
let (output, status) = execCmdEx(fmt"{app.nim} --version")
doAssert status == 0, fmt"make sure {app.nim} is in PATH"
result = output.splitLines()[0]

proc getNimPath(): string =
var which_cmd = "which nim" # POSIX
# TODO: use `which` PENDING https://github.com/nim-lang/Nim/issues/8311
when defined(Windows):
which_cmd = "where nim" # Windows
let which_cmd = fmt"where {app.nim}"
else:
let which_cmd = fmt"which {app.nim}"
let (output, status) = execCmdEx(which_cmd)
if status == 0:
return " at " & output
Expand Down Expand Up @@ -84,7 +95,7 @@ proc showError(output: string) =
buffer.writeLine(shortcut)
buffer.flushFile()

let (output, status) = execCmdEx(compileCmd)
let (output, status) = compileCode()
if status == 0:
let lines = output.splitLines()
stdout.setForegroundColor(fgCyan, true)
Expand All @@ -111,13 +122,13 @@ proc init(preload: string = nil) =
buffer = open(bufferSource, fmWrite)
if preload == nil:
# First dummy compilation so next one is faster
discard execCmdEx(compileCmd)
discard compileCode()
return

buffer.writeLine(preload)
buffer.flushFile()
# Check preloaded file compiles succesfully
let (output, status) = execCmdEx(compileCmd)
let (output, status) = compileCode()
if status == 0:
for line in preload.splitLines:
validCode &= line & "\n"
Expand Down Expand Up @@ -180,7 +191,7 @@ proc runForever() =
continue

# Compile buffer
let (output, status) = execCmdEx(compileCmd)
let (output, status) = compileCode()

# Succesful compilation, expression is valid
if status == 0:
Expand Down Expand Up @@ -215,20 +226,27 @@ proc runForever() =
# Clean up
tempIndentCode = ""

when isMainModule:
# Preload existing source code: inim example.nim
if paramCount() > 0:
let filePath = paramStr(paramCount())
if not filePath.fileExists:
echo "inim: cannot access '", filePath, "': No such file"
quit(1)
if not filePath.endsWith(".nim"):
echo "inim: '", filePath, "' is not a Nim file"
quit(1)
let fileData = getFileData(filePath)
proc main(nim="nim", srcFile = "", showHeader = true) =
## inim interpreter
app.new()
app.nim=nim
app.srcFile=srcFile
app.showHeader=showHeader

if srcFile.len>0:
doAssert(srcFile.fileExists, "cannot access " & srcFile)
doAssert(srcFile.splitFile.ext == ".nim")
let fileData = getFileData(srcFile)
init(fileData)
else:
init() # Clean init

welcomeScreen()
if app.showHeader: welcomeScreen()
runForever()

when isMainModule:
import cligen
dispatch(main, help = {
"nim": "path to nim compiler",
"srcFile": "nim script to run",
"showHeader": "show program info startup",
})

0 comments on commit b0bdebd

Please sign in to comment.