rips 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/example.rips +3 -0
- data/lib/rips/assembler.rb +70 -1
- data/lib/rips/error.rb +4 -1
- data/lib/rips/formats.rb +1 -0
- data/lib/rips/formats/cformat.rb +22 -0
- data/lib/rips/formats/format.rb +2 -0
- data/lib/rips/instructions.rb +1 -0
- data/lib/rips/instructions/instruction.rb +29 -3
- data/lib/rips/instructions/li.rb +19 -0
- data/lib/rips/instructions/set.rb +1 -1
- data/lib/rips/utils.rb +6 -0
- data/lib/rips/variables.rb +2 -0
- data/lib/rips/variables/inmediate.rb +35 -0
- data/lib/rips/variables/register.rb +45 -0
- data/lib/rips/variables/variable.rb +20 -0
- data/lib/rips/version.rb +1 -1
- metadata +7 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9d9774b257b7691b23c8eeb4fcd0b9de10f2515
|
4
|
+
data.tar.gz: 9fff56fe4e0eacebfd306b9d4cb4b1dc9a954fe0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfd2de7a53fe4840c1b66c9002c59b8e04f75473471b6fe80020c9b82f79c0c320dd33a8ba9d3f2860804eb1aef16837802c3991647d3e4ec3b7517c3d59a5a0
|
7
|
+
data.tar.gz: aef9b647fd23285e73e1de83dff021551a2fe042fa2af82c38e4f81c20ee0cd053fe3c4368221a7fe5151dbfdd95294453b1be16ef4a9b696eeda742210bb281
|
data/example.rips
ADDED
data/lib/rips/assembler.rb
CHANGED
@@ -23,15 +23,84 @@ module Rips
|
|
23
23
|
|
24
24
|
# Analyze and translate each instruction
|
25
25
|
def run
|
26
|
+
|
27
|
+
@input.each do |line|
|
28
|
+
|
29
|
+
parse_input(line)
|
30
|
+
exists_instruction
|
31
|
+
|
32
|
+
@instruction = get_instruction
|
33
|
+
|
34
|
+
argument_size
|
35
|
+
argument_syntax
|
36
|
+
|
37
|
+
@instruction.set_arguments(@cmd[:arguments])
|
38
|
+
|
39
|
+
show
|
40
|
+
@output << @instruction.code
|
41
|
+
@line += 1
|
42
|
+
end
|
43
|
+
|
44
|
+
generate
|
26
45
|
end
|
27
46
|
|
28
47
|
# Codification log of instruction
|
29
48
|
def show
|
49
|
+
# Show code with '_' separator
|
50
|
+
puts "Code Instruction: " << @instruction.code.scan(/.{4}|.+/).join("_")
|
30
51
|
end
|
31
52
|
|
32
53
|
# Generate output in "progfile.dat"
|
33
54
|
def generate
|
34
|
-
|
55
|
+
File.open("progfile.dat", "w") do |f|
|
56
|
+
@output.each do |line|
|
57
|
+
f.puts line
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Split on tokens
|
63
|
+
def parse_input (line)
|
64
|
+
@cmd = { name: line.split(' ').first.downcase,
|
65
|
+
arguments: line.split(' ',2).pop.delete(' ').split(',') }
|
66
|
+
end
|
35
67
|
|
68
|
+
# Obtain instruction's instance object
|
69
|
+
def get_instruction
|
70
|
+
Object.const_get("Rips::Instructions::#{@cmd[:name].capitalize}").new
|
71
|
+
end
|
72
|
+
|
73
|
+
# Exists instruction in Instruction Set?
|
74
|
+
def exists_instruction
|
75
|
+
if !Instructions::SET.include? (@cmd[:name])
|
76
|
+
Error::message( 4,
|
77
|
+
@line,
|
78
|
+
@cmd[:name] )
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Check number of arguments given with expected by instruction
|
83
|
+
def argument_size
|
84
|
+
if @cmd[:arguments].size != @instruction.args_number
|
85
|
+
Error::message( 5,
|
86
|
+
@line,
|
87
|
+
@cmd[:name],
|
88
|
+
@cmd[:arguments].size,
|
89
|
+
@instruction.args_number )
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Check if arguments are the same variable type of instruction
|
94
|
+
def argument_syntax
|
95
|
+
@instruction.variables.each_with_index do |var,i|
|
96
|
+
if var.syntax? @cmd[:arguments][i]
|
97
|
+
@cmd[:arguments][i] = var.to_i(@cmd[:arguments][i])
|
98
|
+
else
|
99
|
+
Error::message( 6,
|
100
|
+
@line,
|
101
|
+
var.error(@cmd[:arguments][i]) )
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
36
105
|
end
|
37
106
|
end
|
data/lib/rips/error.rb
CHANGED
@@ -5,7 +5,10 @@ module Rips
|
|
5
5
|
[ "RIPS ERROR (0). Unknown problem.",
|
6
6
|
"RIPS ERROR (1). No input file (try `rips file.rips`).",
|
7
7
|
"RIPS ERROR (2). Doesn't exist file or directory.",
|
8
|
-
"RIPS ERROR (3). Permission denied to read file."
|
8
|
+
"RIPS ERROR (3). Permission denied to read file.",
|
9
|
+
"RIPS ERROR (4). In line %d: doesn't exist `%s` instruction in Instruction Set.",
|
10
|
+
"RIPS ERROR (5). In line %d: for `%s` instruction wrong number of arguments (%d for %d).",
|
11
|
+
"RIPS ERROR (6). In line %d: syntax error, %s." ]
|
9
12
|
|
10
13
|
def self.message (num, *args)
|
11
14
|
puts MESSAGES[num] % args
|
data/lib/rips/formats.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
require "rips/formats/cformat"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "rips/formats/format"
|
2
|
+
|
3
|
+
module Rips
|
4
|
+
module Formats
|
5
|
+
|
6
|
+
class CFormat < Format
|
7
|
+
|
8
|
+
attr_reader :args
|
9
|
+
|
10
|
+
# @args: all instruction's arguments
|
11
|
+
def initialize (opcode)
|
12
|
+
super(opcode,2)
|
13
|
+
@args = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
# Pass all arguments at once
|
17
|
+
def set_arguments (args)
|
18
|
+
@args[:r1], @args[:r2] = args[1], args[0]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/rips/formats/format.rb
CHANGED
data/lib/rips/instructions.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
require "rips/instructions/li"
|
@@ -8,10 +8,36 @@ module Rips
|
|
8
8
|
class Instruction
|
9
9
|
|
10
10
|
# @name: mnemonic name
|
11
|
-
# @
|
12
|
-
def initialize (name,
|
13
|
-
@name,@
|
11
|
+
# @format: instruction format
|
12
|
+
def initialize (name, format)
|
13
|
+
@name,@format = name,format
|
14
|
+
@opcode = format.opcode
|
14
15
|
end
|
16
|
+
|
17
|
+
# Return number of arguments
|
18
|
+
def args_number
|
19
|
+
@format.args_number
|
20
|
+
end
|
21
|
+
|
22
|
+
# Pass all arguments at once
|
23
|
+
def set_arguments (args)
|
24
|
+
@format.set_arguments(args)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Coding to Machine Code
|
28
|
+
def code
|
29
|
+
|
30
|
+
output = [@opcode.to_bin(@opcode.size)]
|
31
|
+
|
32
|
+
@format.args.each do |key,value|
|
33
|
+
output << value.to_bin(@length[key])
|
34
|
+
end
|
35
|
+
|
36
|
+
output.insert(-2,@length[:blank]) if (@length.key? :blank)
|
37
|
+
|
38
|
+
output.reverse.join.to_s
|
39
|
+
end
|
40
|
+
|
15
41
|
end
|
16
42
|
end
|
17
43
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "rips/instructions/instruction"
|
2
|
+
|
3
|
+
module Rips
|
4
|
+
module Instructions
|
5
|
+
|
6
|
+
class Li < Instruction
|
7
|
+
|
8
|
+
attr_reader :variables, :length
|
9
|
+
|
10
|
+
# @variables: types of instruction's variables
|
11
|
+
# @length: length in bits for each variable
|
12
|
+
def initialize
|
13
|
+
super("li",Formats::CFormat.new(0b1010))
|
14
|
+
@variables = [Variables::Register.new, Variables::Inmediate.new]
|
15
|
+
@length = {r2:4, r1:8}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/rips/utils.rb
ADDED
data/lib/rips/variables.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "rips/variables/variable"
|
2
|
+
|
3
|
+
module Rips
|
4
|
+
module Variables
|
5
|
+
|
6
|
+
class Inmediate < Variable
|
7
|
+
|
8
|
+
attr_reader :syntax
|
9
|
+
|
10
|
+
# @syntax: example syntax
|
11
|
+
def initialize
|
12
|
+
super(8)
|
13
|
+
@syntax = "-128...127"
|
14
|
+
end
|
15
|
+
|
16
|
+
# Check input variable syntax
|
17
|
+
def syntax? (value)
|
18
|
+
|
19
|
+
if !number? (value)
|
20
|
+
return false
|
21
|
+
end
|
22
|
+
|
23
|
+
value = value.to_i
|
24
|
+
|
25
|
+
# It should be between syntax range
|
26
|
+
if !value.to_i.between?(-2**(@length-1),2**(@length-1)-1)
|
27
|
+
return false
|
28
|
+
else
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "rips/variables/variable"
|
2
|
+
|
3
|
+
module Rips
|
4
|
+
module Variables
|
5
|
+
|
6
|
+
class Register < Variable
|
7
|
+
|
8
|
+
attr_reader :syntax
|
9
|
+
|
10
|
+
# @syntax: example syntax
|
11
|
+
def initialize
|
12
|
+
super(4)
|
13
|
+
@syntax = "$0-15"
|
14
|
+
end
|
15
|
+
|
16
|
+
# Check input variable syntax
|
17
|
+
def syntax? (value)
|
18
|
+
|
19
|
+
if !register? (value)
|
20
|
+
return false
|
21
|
+
end
|
22
|
+
|
23
|
+
# Get integer part
|
24
|
+
value = value.split('$',2).last
|
25
|
+
|
26
|
+
# If contains more "$"
|
27
|
+
if value.include? "$"
|
28
|
+
return false
|
29
|
+
# It should have only 1 or 2 digits
|
30
|
+
elsif !value.size.between?(1,2)
|
31
|
+
return false
|
32
|
+
# If it have 2 digits, first digit doesn't be zero or negative
|
33
|
+
elsif (value.size == 2) && (!value[0].match(/-|0/).nil?)
|
34
|
+
return false
|
35
|
+
# It should be between syntax range
|
36
|
+
elsif !value.to_i.between?(0, 2**@length-1)
|
37
|
+
return false
|
38
|
+
else
|
39
|
+
return true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -9,6 +9,26 @@ module Rips
|
|
9
9
|
def initialize (length)
|
10
10
|
@length = length
|
11
11
|
end
|
12
|
+
|
13
|
+
# Check if value is a register ($?)
|
14
|
+
def register? (value)
|
15
|
+
value[0] == "$"
|
16
|
+
end
|
17
|
+
|
18
|
+
# Check if value is a valid number (...,-1,0,1...)
|
19
|
+
def number? (value)
|
20
|
+
/\A[-]?\d+\z/ === value
|
21
|
+
end
|
22
|
+
|
23
|
+
# Return integer part of value
|
24
|
+
def to_i (value)
|
25
|
+
value.split('$',2).last.to_i
|
26
|
+
end
|
27
|
+
|
28
|
+
# Return error message about incorrent syntax
|
29
|
+
def error (value)
|
30
|
+
"unexpected `#{value}` (expected a `#{self.class.to_s.split(':').last}` argument like `#{@syntax}`)"
|
31
|
+
end
|
12
32
|
end
|
13
33
|
end
|
14
34
|
end
|
data/lib/rips/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rips
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Madh93
|
@@ -67,15 +67,21 @@ files:
|
|
67
67
|
- README.md
|
68
68
|
- Rakefile
|
69
69
|
- bin/rips
|
70
|
+
- example.rips
|
70
71
|
- lib/rips.rb
|
71
72
|
- lib/rips/assembler.rb
|
72
73
|
- lib/rips/error.rb
|
73
74
|
- lib/rips/formats.rb
|
75
|
+
- lib/rips/formats/cformat.rb
|
74
76
|
- lib/rips/formats/format.rb
|
75
77
|
- lib/rips/instructions.rb
|
76
78
|
- lib/rips/instructions/instruction.rb
|
79
|
+
- lib/rips/instructions/li.rb
|
77
80
|
- lib/rips/instructions/set.rb
|
81
|
+
- lib/rips/utils.rb
|
78
82
|
- lib/rips/variables.rb
|
83
|
+
- lib/rips/variables/inmediate.rb
|
84
|
+
- lib/rips/variables/register.rb
|
79
85
|
- lib/rips/variables/variable.rb
|
80
86
|
- lib/rips/version.rb
|
81
87
|
- rips.gemspec
|