Comments (3)
I think it should be called 'readAll' instead. The implementation for 'readAll' for stdin should be very different from the version that can be implemented for proper files: For proper files we know how big the resulting string will be. IMO this suggests we should provide 2 different implementations depending on "tty". And do we really 'readAll' for stdin? Don't serious applications use the rdstdin module (GNU readline wrapper) anyway?
from nim.
Yeah, that implies stdin
has to be a different type than / subtype of TFile
. My reason foor that was
import macros
echo parseExpr(readall(stdin)).lispRepr()
so I can pipe my syntax tree in there and replace it with the macros for some metaprogramming. I don't thik readLine
will do it here.
from nim.
Tass,
Your problems stem from the fact that you are not understanding clearly the distinction between compile-time and run-time code execution in Nimrod.
When you write top-level statements like:
import macros
echo parseExpr(readall(stdin)).lispRepr()
... Nimrod will treat them as run-time code, so it will attempt to do the following:
- read string from stdin (at run-time)
- parse the string as nimrod code (at run-time) - In order for this to work, the whole Nimrod compiler have to embedded in your executable, something that's obviously undesirable.
Meta-programming in Nimrod is all about compile-time code execution, so what you need is to move your code in an "evaluation context" - This would be code inside macros or computed expressions assigned to constant variables.
For example, here is how you can print at compile-time the AST for a string of nimrod code:
import macros
macro dumpStringAst(s: stmt): stmt =
echo s[1].strVal.parseStmt.lispRepr
dumpStringAst """
echo "hello world"
"""
For this kind of purposes, it's usually more convenient to write a macro that takes a block:
import macros
macro dumpLisp(s: stmt): stmt =
echo s[1].lispRepr
dumpLisp:
echo "hello world"
UPDATE: This macro is now part of the standard library (see also macros.dumpTree)
Currently, the evaluation engine is limited to executing only pure nimrod code (there is no access to functions imported from C). The result of this is that it's not possible to do general I/O inside macros. You have access to a function called slurp
that will read the whole contents of a given file at compile-time.
There are number of planned remedies for this in the long-term:
- Integration of libffi in the evaluation engine that will allow calling arbitrary C functions
- compiling macros to native code in DLLs that will boost their performance besides solving this problem.
If your goal is to have interactive shell where you can inspect various Nimrod AST trees, then you must augment the REPL shell to do this (i.e. nimrod i
).
from nim.
Related Issues (20)
- Nimsuggest - Methods marked with {.base.} trigger error on chk HOT 3
- Ref array causing Segfault HOT 4
- osproc.startProcess should support "not following symlinks" HOT 2
- When `move` meets` sink`, `= sink` hook function is not called HOT 4
- [ignore] bisect HOT 7
- Error: type mismatch: got <typedesc[A]>, but expected one of: A = proc (){.closure.}
- Default object values silently override generic types
- Enum Overloading Regression? HOT 4
- async `and` swallows errors
- Async `finally` swallows errors when it contains an `await` wrapped by `try/except`
- The `when nimvm` statement fails to compile when in a file called nimvm.nim HOT 4
- Regression from 2.0 to `devel` with `dirty` template HOT 2
- Async memory leak when raising exception on ORC HOT 1
- unicode.splitWhitespace() and strutils.splitWhitespace() have different results for ASCII string
- Wrong stack trace when exception is raised in template HOT 1
- Error: cannot evaluate at compile time: foo HOT 4
- nim check crashes
- Any differnece between value types and reference types for `=sink` ? HOT 1
- Simple destructor code gives invalid C HOT 4
- Compiler crashes with infinite recursion for nested generic instantiation with static[int] HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nim.