or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

auto-completion.mdclass-loading.mdcode-interpretation.mdcommand-line.mdindex.mdinteractive-shell.md

code-interpretation.mddocs/

0

# Code Interpretation and Execution

1

2

Scala code compilation and execution engine with Spark context integration. Handles code parsing, compilation, variable binding, import management, and result evaluation with full type safety and distributed execution support.

3

4

## Capabilities

5

6

### SparkIMain Class

7

8

Core interpreter that compiles and executes Scala code within the Spark REPL environment.

9

10

```scala { .api }

11

/**

12

* Core interpreter for Scala code compilation and execution

13

* @param initialSettings Compiler settings

14

* @param out Print writer for output

15

* @param propagateExceptions Whether to propagate exceptions

16

*/

17

@DeveloperApi

18

class SparkIMain(

19

initialSettings: Settings = new Settings(),

20

out: JPrintWriter = new JPrintWriter(Console.out, true),

21

propagateExceptions: Boolean = false

22

)

23

24

/**

25

* Alternative constructors

26

*/

27

def this(settings: Settings)

28

def this()

29

```

30

31

**Usage Examples:**

32

33

```scala

34

import org.apache.spark.repl.SparkIMain

35

import scala.tools.nsc.Settings

36

37

// Create with default settings

38

val interpreter = new SparkIMain()

39

40

// Create with custom settings

41

val settings = new Settings()

42

settings.classpath.value = "/path/to/classes"

43

val customInterpreter = new SparkIMain(settings)

44

45

// Initialize for use

46

interpreter.initializeSynchronous()

47

```

48

49

### Code Execution

50

51

Methods for interpreting and executing Scala code with result tracking.

52

53

```scala { .api }

54

/**

55

* Interpret a line of Scala code

56

* @param line Scala code to execute

57

* @return Result indicating success, error, or incomplete input

58

*/

59

@DeveloperApi

60

def interpret(line: String): Results.Result

61

62

/**

63

* Interpret synthetic (generated) code

64

* @param line Generated Scala code to execute

65

* @return Result indicating execution outcome

66

*/

67

@DeveloperApi

68

def interpretSynthetic(line: String): Results.Result

69

70

/**

71

* Compile Scala code to bytecode

72

* @param code Scala source code

73

* @return true if compilation successful

74

*/

75

@DeveloperApi

76

def compileString(code: String): Boolean

77

78

/**

79

* Compile multiple source files

80

* @param sources Source files to compile

81

* @return true if compilation successful

82

*/

83

@DeveloperApi

84

def compileSources(sources: SourceFile*): Boolean

85

```

86

87

**Usage Examples:**

88

89

```scala

90

import scala.tools.nsc.interpreter.Results

91

92

// Execute simple expressions

93

val result1 = interpreter.interpret("val x = 42")

94

val result2 = interpreter.interpret("x * 2")

95

96

// Handle results

97

result1 match {

98

case Results.Success => println("Variable defined successfully")

99

case Results.Error => println("Error in code")

100

case Results.Incomplete => println("Need more input")

101

}

102

103

// Compile code without execution

104

val compiled = interpreter.compileString("class MyClass { def hello = \"world\" }")

105

```

106

107

### Variable Binding

108

109

Methods for binding Scala values and objects to interpreter variables.

110

111

```scala { .api }

112

/**

113

* Bind a value to a variable name in the interpreter

114

* @param name Variable name

115

* @param boundType Type string for the value

116

* @param value Value to bind

117

* @param modifiers List of modifiers (e.g., "implicit", "lazy")

118

* @return Result indicating binding success

119

*/

120

@DeveloperApi

121

def bind(

122

name: String,

123

boundType: String,

124

value: Any,

125

modifiers: List[String] = Nil

126

): Results.Result

127

128

/**

129

* Direct binding without type checking

130

* @param name Variable name

131

* @param boundType Type string

132

* @param value Value to bind

133

* @return Result indicating binding success

134

*/

135

@DeveloperApi

136

def directBind(name: String, boundType: String, value: Any): Results.Result

137

138

/**

139

* Rebind a named parameter

140

* @param p Named parameter to rebind

141

* @return Result indicating rebinding success

142

*/

143

@DeveloperApi

144

def rebind(p: NamedParam): Results.Result

145

```

146

147

**Usage Examples:**

148

149

```scala

150

// Bind simple values

151

interpreter.bind("myString", "String", "Hello World")

152

interpreter.bind("myInt", "Int", 42)

153

154

// Bind with modifiers

155

interpreter.bind("lazyValue", "String", "computed", List("lazy"))

156

157

// Bind complex objects

158

val sparkContext = new SparkContext()

159

interpreter.bind("sc", "SparkContext", sparkContext)

160

161

// Direct binding

162

interpreter.directBind("directValue", "Double", 3.14)

163

```

164

165

### Import Management

166

167

Methods for managing imports within the interpreter session.

168

169

```scala { .api }

170

/**

171

* Add imports to the interpreter session

172

* @param ids Import identifiers (e.g., "scala.collection.mutable._")

173

* @return Result indicating import success

174

*/

175

@DeveloperApi

176

def addImports(ids: String*): Results.Result

177

```

178

179

**Usage Examples:**

180

181

```scala

182

// Add single import

183

interpreter.addImports("scala.collection.mutable.Map")

184

185

// Add multiple imports

186

interpreter.addImports(

187

"org.apache.spark.sql._",

188

"org.apache.spark.rdd.RDD",

189

"scala.util.{Try, Success, Failure}"

190

)

191

```

192

193

### Session Management

194

195

Methods for managing interpreter session state and lifecycle.

196

197

```scala { .api }

198

/**

199

* Initialize interpreter synchronously

200

*/

201

@DeveloperApi

202

def initializeSynchronous(): Unit

203

204

/**

205

* Reset interpreter to initial state

206

*/

207

@DeveloperApi

208

def reset(): Unit

209

210

/**

211

* Close interpreter and release resources

212

*/

213

@DeveloperApi

214

def close(): Unit

215

216

/**

217

* Get most recently created variable name

218

* @return Variable name string

219

*/

220

@DeveloperApi

221

def mostRecentVar: String

222

```

223

224

**Usage Examples:**

225

226

```scala

227

// Initialize for use

228

interpreter.initializeSynchronous()

229

230

// Reset state

231

interpreter.reset()

232

233

// Get last variable

234

val lastVar = interpreter.mostRecentVar

235

println(s"Last variable: $lastVar")

236

237

// Clean up

238

interpreter.close()

239

```

240

241

### Execution Control

242

243

Methods for controlling code execution environment and output.

244

245

```scala { .api }

246

/**

247

* Execute code block with suppressed output

248

* @param body Code block to execute

249

* @return Result of execution

250

*/

251

@DeveloperApi

252

def beQuietDuring[T](body: => T): T

253

254

/**

255

* Execute code block silently (no output)

256

* @param operation Operation to execute

257

* @return Result of operation

258

*/

259

@DeveloperApi

260

def beSilentDuring[T](operation: => T): T

261

262

/**

263

* Get current execution wrapper code

264

* @return Wrapper code string

265

*/

266

@DeveloperApi

267

def executionWrapper: String

268

269

/**

270

* Set execution wrapper code

271

* @param code Wrapper code to use

272

*/

273

@DeveloperApi

274

def setExecutionWrapper(code: String): Unit

275

276

/**

277

* Clear execution wrapper

278

*/

279

@DeveloperApi

280

def clearExecutionWrapper(): Unit

281

```

282

283

**Usage Examples:**

284

285

```scala

286

// Suppress output during execution

287

val result = interpreter.beQuietDuring {

288

interpreter.interpret("println(\"This won't be shown\")")

289

}

290

291

// Silent execution

292

interpreter.beSilentDuring {

293

interpreter.interpret("val hidden = 42")

294

}

295

296

// Custom execution wrapper

297

interpreter.setExecutionWrapper("try { %s } catch { case e => e.printStackTrace() }")

298

```

299

300

### Type and Symbol Inspection

301

302

Methods for inspecting types, symbols, and definitions within the interpreter.

303

304

```scala { .api }

305

/**

306

* Get value of a term by name

307

* @param id Term identifier

308

* @return Optional value reference

309

*/

310

@DeveloperApi

311

def valueOfTerm(id: String): Option[AnyRef]

312

313

/**

314

* Get class of a term by name

315

* @param id Term identifier

316

* @return Optional class reference

317

*/

318

@DeveloperApi

319

def classOfTerm(id: String): Option[Class[_]]

320

321

/**

322

* Get type of a term by name

323

* @param id Term identifier

324

* @return Type information

325

*/

326

@DeveloperApi

327

def typeOfTerm(id: String): Type

328

329

/**

330

* Get symbol of a term by name

331

* @param id Term identifier

332

* @return Symbol information

333

*/

334

@DeveloperApi

335

def symbolOfTerm(id: String): Symbol

336

337

/**

338

* Get type of an expression

339

* @param expr Expression string

340

* @param silent Whether to suppress error output

341

* @return Type information

342

*/

343

@DeveloperApi

344

def typeOfExpression(expr: String, silent: Boolean = true): Type

345

```

346

347

**Usage Examples:**

348

349

```scala

350

// Inspect bound variables

351

interpreter.bind("test", "String", "hello")

352

353

val value = interpreter.valueOfTerm("test")

354

val clazz = interpreter.classOfTerm("test")

355

val tpe = interpreter.typeOfTerm("test")

356

357

// Get expression type

358

val exprType = interpreter.typeOfExpression("List(1, 2, 3)")

359

println(s"Expression type: $exprType")

360

```

361

362

### Definition Inspection

363

364

Methods for inspecting defined symbols and names within the interpreter session.

365

366

```scala { .api }

367

/**

368

* Get all defined term names

369

* @return List of term names

370

*/

371

@DeveloperApi

372

def definedTerms: List[TermName]

373

374

/**

375

* Get all defined type names

376

* @return List of type names

377

*/

378

@DeveloperApi

379

def definedTypes: List[TypeName]

380

381

/**

382

* Get all defined symbols

383

* @return Set of symbols

384

*/

385

@DeveloperApi

386

def definedSymbols: Set[Symbol]

387

388

/**

389

* Get class symbols

390

* @return List of class symbols

391

*/

392

@DeveloperApi

393

def classSymbols: List[ClassSymbol]

394

395

/**

396

* Get method symbols

397

* @return List of method symbols

398

*/

399

@DeveloperApi

400

def methodSymbols: List[MethodSymbol]

401

```

402

403

### Compiler and ClassPath Management

404

405

Methods for managing the underlying compiler and classpath.

406

407

```scala { .api }

408

/**

409

* Get compiler global instance

410

* @return Compiler global

411

*/

412

@DeveloperApi

413

lazy val global: Global

414

415

/**

416

* Get class output directory

417

* @return Output directory file

418

*/

419

@DeveloperApi

420

lazy val getClassOutputDirectory: File

421

422

/**

423

* Get class server URI for distributed execution

424

* @return URI string

425

*/

426

@DeveloperApi

427

def classServerUri: String

428

429

/**

430

* Add URLs to interpreter classpath

431

* @param urls URLs to add

432

*/

433

@DeveloperApi

434

def addUrlsToClassPath(urls: URL*): Unit

435

436

/**

437

* Check if interpreter is reporting errors

438

* @return true if reporting errors

439

*/

440

@DeveloperApi

441

def isReportingErrors: Boolean

442

```

443

444

**Usage Examples:**

445

446

```scala

447

// Access compiler

448

val compiler = interpreter.global

449

450

// Get output directory

451

val outputDir = interpreter.getClassOutputDirectory

452

println(s"Classes written to: $outputDir")

453

454

// Add to classpath

455

import java.net.URL

456

val jarUrl = new URL("file:///path/to/library.jar")

457

interpreter.addUrlsToClassPath(jarUrl)

458

```