summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/kernel/InstructionBuffer.java
blob: befe0d9b26c1a803c675673f7bc1bc4f11c0607a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Lo�c Le Coq
 * Copyright (C) 2013 Marko Zivkovic
 * 
 * Contact Information: marko88zivkovic at gmail dot com
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.  This program is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 * Public License for more details.  You should have received a copy of the 
 * GNU General Public License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA 02110-1301, USA.
 * 
 * 
 * This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
 * during his Bachelor thesis at the computer science department of ETH Z�rich,
 * in the year 2013 and/or during future work.
 * 
 * It is a reengineered version of XLogo written by Lo�c Le Coq, published
 * under the GPL License at http://xlogo.tuxfamily.org/
 * 
 * Contents of this file were initially written by Lo�c Le Coq,
 * modifications, extensions, refactorings might have been applied by Marko Zivkovic 
 */

package xlogo.kernel;

public class InstructionBuffer {
	/**
	 * This integer represents the max Characters in the StringBuffer
	 */
	
	private final static int MAX_CHARACTERS=15000;
	/**
	 * The main StringBuffer containing instruction
	 */
	private StringBuffer mainBuffer;
	/**
	 * If the main StringBuffer is very large, Xlogo could become very slow. If main StringBuffer should contain more than MAX_CHARACTERS, those characters are saved in this StringBuffer
	 */
	private StringBuffer stock;
	
	InstructionBuffer(){
		clear();
	}
	InstructionBuffer(String s){
		mainBuffer=new StringBuffer(s);
		stock=new StringBuffer();
	}
	/**
	 * Inserts some instructions at the beginning of the StringBuffer mainBuffer 
	 * @param sb: The code to insert
	 */
	void insertCode(StringBuffer sb){
		if (sb.length()>InstructionBuffer.MAX_CHARACTERS) {
			// insert current mainBuffer to stock
			if (mainBuffer.length()!=0) stock.insert(0,mainBuffer);
			// Copy MAX_CHARACTERS into mainBuffer
			mainBuffer=new StringBuffer(sb.substring(0, InstructionBuffer.MAX_CHARACTERS));
			// All remaining characters into stock
			stock.insert(0,sb.substring(InstructionBuffer.MAX_CHARACTERS));
		}
		else {
			mainBuffer.insert(0, sb);
		}
		
	}
	/**
	 * returns the total length of the two Buffer
	 */
	protected int getLength(){
		return mainBuffer.length()+stock.length();
	}
	/**
	 * Inserts the String s at the beginning of the mainBuffer
	 * @param s
	 */
	protected void insert(String s){
		mainBuffer.insert(0, s);
	}
	/**
	 * Search for the String s , first in mainBuffer and then in stock
	 * if it isn't found in any buffer returns -1
	 * @param s: The String to search
	 */
	protected int indexOf(String s){
		int index=mainBuffer.indexOf(s);
		if (index==-1){
			index=stock.indexOf(s);
			if (index==-1) return -1;
			else return index+mainBuffer.length();
		}
		return index;
		
	}
	/**
	 * Search for the String s , first in mainBuffer and then in stock
	 * if not found in any buffer returns -1
	 * @param s: The String to search
	 */
	protected int indexOf(String s, int fromIndex){
		int index=-1;
		if (fromIndex<mainBuffer.length()) index=mainBuffer.indexOf(s,fromIndex);
		if (index==-1){
			int from=0;
			if (fromIndex>=mainBuffer.length()) from=fromIndex-mainBuffer.length();
			index=stock.indexOf(s,from);
			if (index==-1) return -1;
			else return index+mainBuffer.length();
		}
		return index;
		
	}
	
	
	/**
	 * Delete all code from offset start to offset end
	 * @param start Start offset
	 * @param end End offset
	 */
	protected void delete(int start, int end){
		if (end<=mainBuffer.length()) mainBuffer.delete(start, end);
		else {
			stock.delete(0, end-mainBuffer.length());
			mainBuffer=new StringBuffer();
			transferStock();
		}
		if (mainBuffer.length()==0){
			// if there are instruction in stock yet
			if (stock.length()!=0) transferStock();
		}
		
	}
	/**
	 * Transfers MAX_CHARCATERS from the buffer stock to mainBuffer
	 */
	
	private void transferStock(){
		if (stock.length()>InstructionBuffer.MAX_CHARACTERS){
			mainBuffer.append(stock.substring(0, InstructionBuffer.MAX_CHARACTERS));			
			stock.delete(0, InstructionBuffer.MAX_CHARACTERS);
		}
		else {
			mainBuffer.append(stock);
			stock=new StringBuffer();
		}
	}
	/**
	 * Returns next Word 
	 * @return a String which represents the next word
	 */
	protected String getNextWord() {
		StringBuffer mot = new StringBuffer();
		char caractere;
		for (int i = 0; i < mainBuffer.length(); i++) {
			caractere = mainBuffer.charAt(i);
			if (caractere == ' ') {
				return mot.toString();
			} else
				mot.append(caractere);
			if (i==mainBuffer.length()-1&& stock.length()!=0){
				transferStock();
			}
		}
		// System.out.println("mot: "+mot);
		return mot.toString();
	}
 /**
  * Deletes the String mot from the mainBuffer instructions
  * @param mot The string to delete
  */
	protected void deleteFirstWord(String mot) {
		if (mainBuffer.length() > mot.length())
			mainBuffer = mainBuffer.delete(0, mot.length() + 1);
		else
			mainBuffer = new StringBuffer();
		if (mainBuffer.length()==0){
			// if there are instruction in stock yet
			if (stock.length()!=0) transferStock();
		}
	}
	/**
	 * Return Character at the chosen index. this methos search first in mainBuffer and then in stock
	 * @param index The chosen index
	 * @return A character
	 */
	protected char charAt(int index){
		if (index<mainBuffer.length()) return mainBuffer.charAt(index);
		return stock.charAt(index-mainBuffer.length());
		
	}
	/**
	 * Clear the both buffers
	 */
	public void clear(){
		mainBuffer=new StringBuffer();
		stock=new StringBuffer();
	}
	public String toString(){
		return mainBuffer.toString()+stock.toString();
//		return "MainBuffer:"+mainBuffer.toString()+":stock:"+stock.toString();
	}
}