Interactive D?

Oskar Linde oskar.lindeREM at OVEgmail.com
Fri Feb 22 07:21:42 PST 2008


I got the idea to investigate the possibility of an interactive D 
interpreter. A lazy afternoon of hacking resulted in a proof of concept, 
and what follows is a short demo.

Could there be any interest in pursuing this?

(# --- are just comments)

$ ./interactiveD
Interactive D version 0.0.1
Type "help" for more information.

# --- Basic expressions

 >>> 1+1

  ans = 2 (int)

 >>> x = 2

  x = 2 (int)

# --- Dynamic types

 >>> x = x * 5.0

  x = 10 (double)

 >>> x++

  ans = 10 (double)

# --- Variables

 >>> who

Variables:
  x = 11 (double)
  ans = 10 (double)

# --- Error recovery

 >>> foo(5)

input:1: Error: undefined identifier foo
input:1: Error: function expected before (), not foo of type int
Failed to evaluate

# --- Dynamic module imports (and on the fly compilation)

 >>> import test

 >>> foo(5)

  ans = 26 (int)

# --- Automatic dependency checking and dynamic recompilation

 >>> foo(5)

test.d changed on disk. Recompiling.
  ans = 25 (int)

# --- Standard library access

 >>> import std.math

 >>> cos(1.0)

  ans = 0.540302 (real)

 >>> import std.stdio

 >>> writefln("Hello World")

Hello World

# --- UDTs

 >>> a = Vec(1,5)

  a = [1,5] (test.Vec)

 >>> b = Vec(3,2)

  b = [3,2] (test.Vec)

 >>> a+b

  ans = [4,7] (test.Vec)

 >>> import

Imports:
  test
  std.math

 >>> import std.string

# --- Bugs :)

 >>> "a b c d e".split()

  ans = [l,e,.,,s] (char[][])

 >>> "a b c d e".dup.split()

  ans = [a,b,c,d,e] (char[][])

# --- Templates

 >>> import multiarray

 >>> x = ArraySlice!(int,2)(3,4)

  x = (3x4)
       0        0        0
       0        0        0
       0        0        0
       0        0        0
  (multiarray.ArraySlice!(int,2))

 >>> x.diag[] = 1

   ans = (3)
       1       1       1
  (multiarray.ArraySlice!(int,1))

 >>> x

  ans = (3x4)
       1        0        0
       0        1        0
       0        0        1
       0        0        0
  (multiarray.ArraySlice!(int,2))

 >>> x.transpose()

 >>> x

  ans = (4x3)
       1        0        0        0
       0        1        0        0
       0        0        1        0
  (multiarray.ArraySlice!(int,2))

 >>> x[range(0,$), 1] = 2

   ans = (4)
       2       2       2       2
  (multiarray.ArraySlice!(int,1))

 >>> x

  ans = (4x3)
       1        0        0        0
       2        2        2        2
       0        0        1        0
  (multiarray.ArraySlice!(int,2))


-- 
Oskar



More information about the Digitalmars-d mailing list