All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Public Attributes | List of all members
uscript::Chunk Class Reference

#include <chunk.h>

Public Member Functions

unsigned AddConstant (Value constant)
 
void Write (uint8_t instruction)
 
void Disassemble (const std::string &name) const
 
unsigned DisassembleInstruction (unsigned index) const
 

Public Attributes

std::string source
 
std::vector< uint8_t > code
 
std::vector< Valueconstants
 

Detailed Description

Definition at line 45 of file chunk.h.

Member Function Documentation

unsigned uscript::Chunk::AddConstant ( uscript::Value  constant)

Definition at line 10 of file chunk.cc.

10  {
11  constants.push_back(constant);
12  return constants.size() - 1;
13 }
std::vector< Value > constants
Definition: chunk.h:53
void uscript::Chunk::Disassemble ( const std::string &  name) const

Definition at line 15 of file chunk.cc.

15  {
16  std::cout << "== " << name << " ==" << std::endl;
17  for (unsigned index = 0; index < code.size();) {
18  index = DisassembleInstruction(index);
19  }
20 }
std::vector< uint8_t > code
Definition: chunk.h:52
unsigned DisassembleInstruction(unsigned index) const
Definition: chunk.cc:47
then echo fcl name
BEGIN_PROLOG could also be cout
unsigned uscript::Chunk::DisassembleInstruction ( unsigned  index) const

Definition at line 47 of file chunk.cc.

47  {
48  std::cout << index << " ";
49  uint8_t instruction = code[index];
50  switch (instruction) {
51  case uscript::OP_RETURN:
52  return simpleInstruction("OP_RETURN", index);
53  case uscript::OP_PRINT:
54  return simpleInstruction("OP_PRINT", index);
55  case uscript::OP_FIELDS:
56  return simpleInstruction("OP_FIELDS", index);
57  case uscript::OP_LENGTH:
58  return simpleInstruction("OP_LENGTH", index);
60  return constantInstruction("OP_CONSTANT", index, code[index+1], constants[code[index+1]]);
61  case uscript::OP_ADD:
62  return simpleInstruction("OP_ADD", index);
64  return simpleInstruction("OP_SUBTRACT", index);
66  return simpleInstruction("OP_MULTIPLY", index);
67  case uscript::OP_DIVIDE:
68  return simpleInstruction("OP_DIVIDE", index);
69  case uscript::OP_NEGATE:
70  return simpleInstruction("OP_NEGATE", index);
71  case uscript::OP_NIL:
72  return simpleInstruction("OP_NIL", index);
73  case uscript::OP_NOT:
74  return simpleInstruction("OP_NOT", index);
75  case uscript::OP_TRUE:
76  return simpleInstruction("OP_TRUE", index);
77  case uscript::OP_FALSE:
78  return simpleInstruction("OP_FALSE", index);
79  case uscript::OP_POP:
80  return simpleInstruction("OP_POP", index);
81  case uscript::OP_LOOP:
82  return jumpInstruction("OP_LOOP", -1, index, code[index+1], code[index+2]);
83  case uscript::OP_JUMP:
84  return jumpInstruction("OP_JUMP", 1, index, code[index+1], code[index+2]);
86  return jumpInstruction("OP_JUMP_IF_FALSE", 1, index, code[index+1], code[index+2]);
88  return constantInstruction("OP_GET_PROPERTY", index, code[index+1], constants[code[index+1]]);
89  case uscript::OP_CALL:
90  return byteInstruction("OP_CALL", index, code[index+1]);
91  case uscript::OP_INDEX:
92  return simpleInstruction("OP_INDEX", index);
94  return byteInstruction("OP_SET_LOCAL", index, code[index+1]);
96  return byteInstruction("OP_GET_LOCAL", index, code[index+1]);
98  return constantInstruction("OP_DEFINE_GLOBAL", index, code[index+1], constants[code[index+1]]);
100  return constantInstruction("OP_GET_GLOBAL", index, code[index+1], constants[code[index+1]]);
102  return constantInstruction("OP_SET_GLOBAL", index, code[index+1], constants[code[index+1]]);
103  case uscript::OP_EQUAL:
104  return simpleInstruction("OP_EQUAL", index);
105  case uscript::OP_GREATER:
106  return simpleInstruction("OP_GREATER", index);
107  case uscript::OP_LESS:
108  return simpleInstruction("OP_LESS", index);
109  default:
110  std::cout << "Unknown Instruction: " << instruction << std::endl;
111  return index + 1;
112  }
113 }
std::vector< Value > constants
Definition: chunk.h:53
unsigned jumpInstruction(const char *name, int sign, unsigned index, uint8_t jumpa, uint8_t jumpb)
Definition: chunk.cc:27
unsigned simpleInstruction(const char *name, unsigned index)
Definition: chunk.cc:22
unsigned constantInstruction(const char *name, unsigned index, uint8_t constant, const uscript::Value &value)
Definition: chunk.cc:35
std::vector< uint8_t > code
Definition: chunk.h:52
BEGIN_PROLOG could also be cout
unsigned byteInstruction(const char *name, unsigned index, uint8_t slot)
Definition: chunk.cc:42
void uscript::Chunk::Write ( uint8_t  instruction)

Definition at line 6 of file chunk.cc.

6  {
7  code.push_back(instruction);
8 }
std::vector< uint8_t > code
Definition: chunk.h:52

Member Data Documentation

std::vector<uint8_t> uscript::Chunk::code

Definition at line 52 of file chunk.h.

std::vector<Value> uscript::Chunk::constants

Definition at line 53 of file chunk.h.

std::string uscript::Chunk::source

Definition at line 51 of file chunk.h.


The documentation for this class was generated from the following files: